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 | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 45 | struct pyra_control control; |
| 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 | |
| 52 | control.command = PYRA_COMMAND_CONTROL; |
| 53 | control.value = value; |
| 54 | control.request = request; |
| 55 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 56 | return roccat_common_send(usb_dev, PYRA_USB_COMMAND_CONTROL, |
| 57 | &control, sizeof(struct pyra_control)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static int pyra_receive_control_status(struct usb_device *usb_dev) |
| 61 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 62 | int retval; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 63 | struct pyra_control control; |
| 64 | |
| 65 | do { |
| 66 | msleep(10); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 67 | retval = roccat_common_receive(usb_dev, PYRA_USB_COMMAND_CONTROL, |
| 68 | &control, sizeof(struct pyra_control)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 69 | |
| 70 | /* requested too early, try again */ |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 71 | } while (retval == -EPROTO); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 72 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 73 | if (!retval && control.command == PYRA_COMMAND_CONTROL && |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 74 | control.request == PYRA_CONTROL_REQUEST_STATUS && |
| 75 | control.value == 1) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 76 | return 0; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 77 | else { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 78 | hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n", |
| 79 | control.request, control.value); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 80 | return retval ? retval : -EINVAL; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | |
| 84 | static int pyra_get_profile_settings(struct usb_device *usb_dev, |
| 85 | struct pyra_profile_settings *buf, int number) |
| 86 | { |
| 87 | int retval; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 88 | retval = pyra_send_control(usb_dev, number, |
| 89 | PYRA_CONTROL_REQUEST_PROFILE_SETTINGS); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 90 | if (retval) |
| 91 | return retval; |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 92 | return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_PROFILE_SETTINGS, |
| 93 | buf, sizeof(struct pyra_profile_settings)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | static int pyra_get_profile_buttons(struct usb_device *usb_dev, |
| 97 | struct pyra_profile_buttons *buf, int number) |
| 98 | { |
| 99 | int retval; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 100 | retval = pyra_send_control(usb_dev, number, |
| 101 | PYRA_CONTROL_REQUEST_PROFILE_BUTTONS); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 102 | if (retval) |
| 103 | return retval; |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 104 | return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_PROFILE_BUTTONS, |
| 105 | buf, sizeof(struct pyra_profile_buttons)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | static int pyra_get_settings(struct usb_device *usb_dev, |
| 109 | struct pyra_settings *buf) |
| 110 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 111 | return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_SETTINGS, |
| 112 | buf, sizeof(struct pyra_settings)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf) |
| 116 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 117 | return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_INFO, |
| 118 | buf, sizeof(struct pyra_info)); |
| 119 | } |
| 120 | |
| 121 | static int pyra_send(struct usb_device *usb_dev, uint command, |
| 122 | void const *buf, uint size) |
| 123 | { |
| 124 | int retval; |
| 125 | retval = roccat_common_send(usb_dev, command, buf, size); |
| 126 | if (retval) |
| 127 | return retval; |
| 128 | return pyra_receive_control_status(usb_dev); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static int pyra_set_profile_settings(struct usb_device *usb_dev, |
| 132 | struct pyra_profile_settings const *settings) |
| 133 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 134 | return pyra_send(usb_dev, PYRA_USB_COMMAND_PROFILE_SETTINGS, settings, |
| 135 | sizeof(struct pyra_profile_settings)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static int pyra_set_profile_buttons(struct usb_device *usb_dev, |
| 139 | struct pyra_profile_buttons const *buttons) |
| 140 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 141 | return pyra_send(usb_dev, PYRA_USB_COMMAND_PROFILE_BUTTONS, buttons, |
| 142 | sizeof(struct pyra_profile_buttons)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static int pyra_set_settings(struct usb_device *usb_dev, |
| 146 | struct pyra_settings const *settings) |
| 147 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 148 | int retval; |
| 149 | retval = roccat_common_send(usb_dev, PYRA_USB_COMMAND_SETTINGS, settings, |
| 150 | sizeof(struct pyra_settings)); |
| 151 | if (retval) |
| 152 | return retval; |
| 153 | return pyra_receive_control_status(usb_dev); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp, |
| 157 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 158 | loff_t off, size_t count) |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 159 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 160 | struct device *dev = |
| 161 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 162 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
| 163 | |
| 164 | if (off >= sizeof(struct pyra_profile_settings)) |
| 165 | return 0; |
| 166 | |
| 167 | if (off + count > sizeof(struct pyra_profile_settings)) |
| 168 | count = sizeof(struct pyra_profile_settings) - off; |
| 169 | |
| 170 | mutex_lock(&pyra->pyra_lock); |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 171 | memcpy(buf, ((char const *)&pyra->profile_settings[*(uint *)(attr->private)]) + off, |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 172 | count); |
| 173 | mutex_unlock(&pyra->pyra_lock); |
| 174 | |
| 175 | return count; |
| 176 | } |
| 177 | |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 178 | static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp, |
| 179 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 180 | loff_t off, size_t count) |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 181 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 182 | struct device *dev = |
| 183 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 184 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
| 185 | |
| 186 | if (off >= sizeof(struct pyra_profile_buttons)) |
| 187 | return 0; |
| 188 | |
| 189 | if (off + count > sizeof(struct pyra_profile_buttons)) |
| 190 | count = sizeof(struct pyra_profile_buttons) - off; |
| 191 | |
| 192 | mutex_lock(&pyra->pyra_lock); |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 193 | memcpy(buf, ((char const *)&pyra->profile_buttons[*(uint *)(attr->private)]) + off, |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 194 | count); |
| 195 | mutex_unlock(&pyra->pyra_lock); |
| 196 | |
| 197 | return count; |
| 198 | } |
| 199 | |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 200 | static ssize_t pyra_sysfs_write_profile_settings(struct file *fp, |
| 201 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 202 | loff_t off, size_t count) |
| 203 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 204 | struct device *dev = |
| 205 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 206 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
| 207 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 208 | int retval = 0; |
| 209 | int difference; |
| 210 | int profile_number; |
| 211 | struct pyra_profile_settings *profile_settings; |
| 212 | |
| 213 | if (off != 0 || count != sizeof(struct pyra_profile_settings)) |
| 214 | return -EINVAL; |
| 215 | |
| 216 | profile_number = ((struct pyra_profile_settings const *)buf)->number; |
| 217 | profile_settings = &pyra->profile_settings[profile_number]; |
| 218 | |
| 219 | mutex_lock(&pyra->pyra_lock); |
| 220 | difference = memcmp(buf, profile_settings, |
| 221 | sizeof(struct pyra_profile_settings)); |
| 222 | if (difference) { |
| 223 | retval = pyra_set_profile_settings(usb_dev, |
| 224 | (struct pyra_profile_settings const *)buf); |
| 225 | if (!retval) |
| 226 | memcpy(profile_settings, buf, |
| 227 | sizeof(struct pyra_profile_settings)); |
| 228 | } |
| 229 | mutex_unlock(&pyra->pyra_lock); |
| 230 | |
| 231 | if (retval) |
| 232 | return retval; |
| 233 | |
| 234 | return sizeof(struct pyra_profile_settings); |
| 235 | } |
| 236 | |
| 237 | static ssize_t pyra_sysfs_write_profile_buttons(struct file *fp, |
| 238 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 239 | loff_t off, size_t count) |
| 240 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 241 | struct device *dev = |
| 242 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 243 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
| 244 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 245 | int retval = 0; |
| 246 | int difference; |
| 247 | int profile_number; |
| 248 | struct pyra_profile_buttons *profile_buttons; |
| 249 | |
| 250 | if (off != 0 || count != sizeof(struct pyra_profile_buttons)) |
| 251 | return -EINVAL; |
| 252 | |
| 253 | profile_number = ((struct pyra_profile_buttons const *)buf)->number; |
| 254 | profile_buttons = &pyra->profile_buttons[profile_number]; |
| 255 | |
| 256 | mutex_lock(&pyra->pyra_lock); |
| 257 | difference = memcmp(buf, profile_buttons, |
| 258 | sizeof(struct pyra_profile_buttons)); |
| 259 | if (difference) { |
| 260 | retval = pyra_set_profile_buttons(usb_dev, |
| 261 | (struct pyra_profile_buttons const *)buf); |
| 262 | if (!retval) |
| 263 | memcpy(profile_buttons, buf, |
| 264 | sizeof(struct pyra_profile_buttons)); |
| 265 | } |
| 266 | mutex_unlock(&pyra->pyra_lock); |
| 267 | |
| 268 | if (retval) |
| 269 | return retval; |
| 270 | |
| 271 | return sizeof(struct pyra_profile_buttons); |
| 272 | } |
| 273 | |
| 274 | static ssize_t pyra_sysfs_read_settings(struct file *fp, |
| 275 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 276 | loff_t off, size_t count) |
| 277 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 278 | struct device *dev = |
| 279 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 280 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
| 281 | |
| 282 | if (off >= sizeof(struct pyra_settings)) |
| 283 | return 0; |
| 284 | |
| 285 | if (off + count > sizeof(struct pyra_settings)) |
| 286 | count = sizeof(struct pyra_settings) - off; |
| 287 | |
| 288 | mutex_lock(&pyra->pyra_lock); |
| 289 | memcpy(buf, ((char const *)&pyra->settings) + off, count); |
| 290 | mutex_unlock(&pyra->pyra_lock); |
| 291 | |
| 292 | return count; |
| 293 | } |
| 294 | |
| 295 | static ssize_t pyra_sysfs_write_settings(struct file *fp, |
| 296 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 297 | loff_t off, size_t count) |
| 298 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 299 | struct device *dev = |
| 300 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 301 | struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev)); |
| 302 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 303 | int retval = 0; |
| 304 | int difference; |
| 305 | |
| 306 | if (off != 0 || count != sizeof(struct pyra_settings)) |
| 307 | return -EINVAL; |
| 308 | |
| 309 | mutex_lock(&pyra->pyra_lock); |
| 310 | difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings)); |
| 311 | if (difference) { |
| 312 | retval = pyra_set_settings(usb_dev, |
| 313 | (struct pyra_settings const *)buf); |
| 314 | if (!retval) |
| 315 | memcpy(&pyra->settings, buf, |
| 316 | sizeof(struct pyra_settings)); |
| 317 | } |
| 318 | mutex_unlock(&pyra->pyra_lock); |
| 319 | |
| 320 | if (retval) |
| 321 | return retval; |
| 322 | |
| 323 | profile_activated(pyra, pyra->settings.startup_profile); |
| 324 | |
| 325 | return sizeof(struct pyra_settings); |
| 326 | } |
| 327 | |
| 328 | |
| 329 | static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev, |
| 330 | struct device_attribute *attr, char *buf) |
| 331 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 332 | struct pyra_device *pyra = |
| 333 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 334 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi); |
| 335 | } |
| 336 | |
| 337 | static ssize_t pyra_sysfs_show_actual_profile(struct device *dev, |
| 338 | struct device_attribute *attr, char *buf) |
| 339 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 340 | struct pyra_device *pyra = |
| 341 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 342 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile); |
| 343 | } |
| 344 | |
| 345 | static ssize_t pyra_sysfs_show_firmware_version(struct device *dev, |
| 346 | struct device_attribute *attr, char *buf) |
| 347 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 348 | struct pyra_device *pyra = |
| 349 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 350 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version); |
| 351 | } |
| 352 | |
| 353 | static ssize_t pyra_sysfs_show_startup_profile(struct device *dev, |
| 354 | struct device_attribute *attr, char *buf) |
| 355 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 356 | struct pyra_device *pyra = |
| 357 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 358 | return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile); |
| 359 | } |
| 360 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 361 | static struct device_attribute pyra_attributes[] = { |
| 362 | __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL), |
| 363 | __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL), |
| 364 | __ATTR(firmware_version, 0440, |
| 365 | pyra_sysfs_show_firmware_version, NULL), |
| 366 | __ATTR(startup_profile, 0440, |
| 367 | pyra_sysfs_show_startup_profile, NULL), |
| 368 | __ATTR_NULL |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 369 | }; |
| 370 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 371 | static struct bin_attribute pyra_bin_attributes[] = { |
| 372 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 373 | .attr = { .name = "profile_settings", .mode = 0220 }, |
| 374 | .size = sizeof(struct pyra_profile_settings), |
| 375 | .write = pyra_sysfs_write_profile_settings |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 376 | }, |
| 377 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 378 | .attr = { .name = "profile1_settings", .mode = 0440 }, |
| 379 | .size = sizeof(struct pyra_profile_settings), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 380 | .read = pyra_sysfs_read_profilex_settings, |
| 381 | .private = &profile_numbers[0] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 382 | }, |
| 383 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 384 | .attr = { .name = "profile2_settings", .mode = 0440 }, |
| 385 | .size = sizeof(struct pyra_profile_settings), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 386 | .read = pyra_sysfs_read_profilex_settings, |
| 387 | .private = &profile_numbers[1] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 388 | }, |
| 389 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 390 | .attr = { .name = "profile3_settings", .mode = 0440 }, |
| 391 | .size = sizeof(struct pyra_profile_settings), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 392 | .read = pyra_sysfs_read_profilex_settings, |
| 393 | .private = &profile_numbers[2] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 394 | }, |
| 395 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 396 | .attr = { .name = "profile4_settings", .mode = 0440 }, |
| 397 | .size = sizeof(struct pyra_profile_settings), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 398 | .read = pyra_sysfs_read_profilex_settings, |
| 399 | .private = &profile_numbers[3] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 400 | }, |
| 401 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 402 | .attr = { .name = "profile5_settings", .mode = 0440 }, |
| 403 | .size = sizeof(struct pyra_profile_settings), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 404 | .read = pyra_sysfs_read_profilex_settings, |
| 405 | .private = &profile_numbers[4] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 406 | }, |
| 407 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 408 | .attr = { .name = "profile_buttons", .mode = 0220 }, |
| 409 | .size = sizeof(struct pyra_profile_buttons), |
| 410 | .write = pyra_sysfs_write_profile_buttons |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 411 | }, |
| 412 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 413 | .attr = { .name = "profile1_buttons", .mode = 0440 }, |
| 414 | .size = sizeof(struct pyra_profile_buttons), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 415 | .read = pyra_sysfs_read_profilex_buttons, |
| 416 | .private = &profile_numbers[0] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 417 | }, |
| 418 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 419 | .attr = { .name = "profile2_buttons", .mode = 0440 }, |
| 420 | .size = sizeof(struct pyra_profile_buttons), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 421 | .read = pyra_sysfs_read_profilex_buttons, |
| 422 | .private = &profile_numbers[1] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 423 | }, |
| 424 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 425 | .attr = { .name = "profile3_buttons", .mode = 0440 }, |
| 426 | .size = sizeof(struct pyra_profile_buttons), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 427 | .read = pyra_sysfs_read_profilex_buttons, |
| 428 | .private = &profile_numbers[2] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 429 | }, |
| 430 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 431 | .attr = { .name = "profile4_buttons", .mode = 0440 }, |
| 432 | .size = sizeof(struct pyra_profile_buttons), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 433 | .read = pyra_sysfs_read_profilex_buttons, |
| 434 | .private = &profile_numbers[3] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 435 | }, |
| 436 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 437 | .attr = { .name = "profile5_buttons", .mode = 0440 }, |
| 438 | .size = sizeof(struct pyra_profile_buttons), |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 439 | .read = pyra_sysfs_read_profilex_buttons, |
| 440 | .private = &profile_numbers[4] |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 441 | }, |
| 442 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 443 | .attr = { .name = "settings", .mode = 0660 }, |
| 444 | .size = sizeof(struct pyra_settings), |
| 445 | .read = pyra_sysfs_read_settings, |
| 446 | .write = pyra_sysfs_write_settings |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 447 | }, |
| 448 | __ATTR_NULL |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 449 | }; |
| 450 | |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 451 | static int pyra_init_pyra_device_struct(struct usb_device *usb_dev, |
| 452 | struct pyra_device *pyra) |
| 453 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 454 | struct pyra_info info; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 455 | int retval, i; |
| 456 | |
| 457 | mutex_init(&pyra->pyra_lock); |
| 458 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 459 | retval = pyra_get_info(usb_dev, &info); |
| 460 | if (retval) |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 461 | return retval; |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 462 | |
| 463 | pyra->firmware_version = info.firmware_version; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 464 | |
| 465 | retval = pyra_get_settings(usb_dev, &pyra->settings); |
| 466 | if (retval) |
| 467 | return retval; |
| 468 | |
| 469 | for (i = 0; i < 5; ++i) { |
| 470 | retval = pyra_get_profile_settings(usb_dev, |
| 471 | &pyra->profile_settings[i], i); |
| 472 | if (retval) |
| 473 | return retval; |
| 474 | |
| 475 | retval = pyra_get_profile_buttons(usb_dev, |
| 476 | &pyra->profile_buttons[i], i); |
| 477 | if (retval) |
| 478 | return retval; |
| 479 | } |
| 480 | |
| 481 | profile_activated(pyra, pyra->settings.startup_profile); |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static int pyra_init_specials(struct hid_device *hdev) |
| 487 | { |
| 488 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 489 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 490 | struct pyra_device *pyra; |
| 491 | int retval; |
| 492 | |
| 493 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 494 | == USB_INTERFACE_PROTOCOL_MOUSE) { |
| 495 | |
| 496 | pyra = kzalloc(sizeof(*pyra), GFP_KERNEL); |
| 497 | if (!pyra) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 498 | hid_err(hdev, "can't alloc device descriptor\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 499 | return -ENOMEM; |
| 500 | } |
| 501 | hid_set_drvdata(hdev, pyra); |
| 502 | |
| 503 | retval = pyra_init_pyra_device_struct(usb_dev, pyra); |
| 504 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 505 | hid_err(hdev, "couldn't init struct pyra_device\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 506 | goto exit_free; |
| 507 | } |
| 508 | |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 509 | retval = roccat_connect(pyra_class, hdev, |
| 510 | sizeof(struct pyra_roccat_report)); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 511 | if (retval < 0) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 512 | hid_err(hdev, "couldn't init char dev\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 513 | } else { |
| 514 | pyra->chrdev_minor = retval; |
| 515 | pyra->roccat_claimed = 1; |
| 516 | } |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 517 | } else { |
| 518 | hid_set_drvdata(hdev, NULL); |
| 519 | } |
| 520 | |
| 521 | return 0; |
| 522 | exit_free: |
| 523 | kfree(pyra); |
| 524 | return retval; |
| 525 | } |
| 526 | |
| 527 | static void pyra_remove_specials(struct hid_device *hdev) |
| 528 | { |
| 529 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 530 | struct pyra_device *pyra; |
| 531 | |
| 532 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 533 | == USB_INTERFACE_PROTOCOL_MOUSE) { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 534 | pyra = hid_get_drvdata(hdev); |
| 535 | if (pyra->roccat_claimed) |
| 536 | roccat_disconnect(pyra->chrdev_minor); |
| 537 | kfree(hid_get_drvdata(hdev)); |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 542 | { |
| 543 | int retval; |
| 544 | |
| 545 | retval = hid_parse(hdev); |
| 546 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 547 | hid_err(hdev, "parse failed\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 548 | goto exit; |
| 549 | } |
| 550 | |
| 551 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 552 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 553 | hid_err(hdev, "hw start failed\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 554 | goto exit; |
| 555 | } |
| 556 | |
| 557 | retval = pyra_init_specials(hdev); |
| 558 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 559 | hid_err(hdev, "couldn't install mouse\n"); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 560 | goto exit_stop; |
| 561 | } |
| 562 | return 0; |
| 563 | |
| 564 | exit_stop: |
| 565 | hid_hw_stop(hdev); |
| 566 | exit: |
| 567 | return retval; |
| 568 | } |
| 569 | |
| 570 | static void pyra_remove(struct hid_device *hdev) |
| 571 | { |
| 572 | pyra_remove_specials(hdev); |
| 573 | hid_hw_stop(hdev); |
| 574 | } |
| 575 | |
| 576 | static void pyra_keep_values_up_to_date(struct pyra_device *pyra, |
| 577 | u8 const *data) |
| 578 | { |
| 579 | struct pyra_mouse_event_button const *button_event; |
| 580 | |
| 581 | switch (data[0]) { |
| 582 | case PYRA_MOUSE_REPORT_NUMBER_BUTTON: |
| 583 | button_event = (struct pyra_mouse_event_button const *)data; |
| 584 | switch (button_event->type) { |
| 585 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2: |
| 586 | profile_activated(pyra, button_event->data1 - 1); |
| 587 | break; |
| 588 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI: |
| 589 | pyra->actual_cpi = button_event->data1; |
| 590 | break; |
| 591 | } |
| 592 | break; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | static void pyra_report_to_chrdev(struct pyra_device const *pyra, |
| 597 | u8 const *data) |
| 598 | { |
| 599 | struct pyra_roccat_report roccat_report; |
| 600 | struct pyra_mouse_event_button const *button_event; |
| 601 | |
| 602 | if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON) |
| 603 | return; |
| 604 | |
| 605 | button_event = (struct pyra_mouse_event_button const *)data; |
| 606 | |
| 607 | switch (button_event->type) { |
| 608 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2: |
| 609 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI: |
| 610 | roccat_report.type = button_event->type; |
| 611 | roccat_report.value = button_event->data1; |
| 612 | roccat_report.key = 0; |
| 613 | roccat_report_event(pyra->chrdev_minor, |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 614 | (uint8_t const *)&roccat_report); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 615 | break; |
| 616 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO: |
| 617 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT: |
| 618 | case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH: |
| 619 | if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) { |
| 620 | roccat_report.type = button_event->type; |
| 621 | roccat_report.key = button_event->data1; |
Stefan Achatz | d2b570a | 2010-09-01 12:42:23 +0200 | [diff] [blame] | 622 | /* |
| 623 | * pyra reports profile numbers with range 1-5. |
| 624 | * Keeping this behaviour. |
| 625 | */ |
| 626 | roccat_report.value = pyra->actual_profile + 1; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 627 | roccat_report_event(pyra->chrdev_minor, |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 628 | (uint8_t const *)&roccat_report); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 629 | } |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 635 | u8 *data, int size) |
| 636 | { |
| 637 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 638 | struct pyra_device *pyra = hid_get_drvdata(hdev); |
| 639 | |
| 640 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 641 | != USB_INTERFACE_PROTOCOL_MOUSE) |
| 642 | return 0; |
| 643 | |
| 644 | pyra_keep_values_up_to_date(pyra, data); |
| 645 | |
| 646 | if (pyra->roccat_claimed) |
| 647 | pyra_report_to_chrdev(pyra, data); |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | static const struct hid_device_id pyra_devices[] = { |
| 653 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, |
| 654 | USB_DEVICE_ID_ROCCAT_PYRA_WIRED) }, |
Stefan Achatz | 3fce224 | 2011-03-23 18:11:36 +0100 | [diff] [blame] | 655 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, |
| 656 | USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) }, |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 657 | { } |
| 658 | }; |
| 659 | |
| 660 | MODULE_DEVICE_TABLE(hid, pyra_devices); |
| 661 | |
| 662 | static struct hid_driver pyra_driver = { |
| 663 | .name = "pyra", |
| 664 | .id_table = pyra_devices, |
| 665 | .probe = pyra_probe, |
| 666 | .remove = pyra_remove, |
| 667 | .raw_event = pyra_raw_event |
| 668 | }; |
| 669 | |
| 670 | static int __init pyra_init(void) |
| 671 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 672 | int retval; |
| 673 | |
| 674 | /* class name has to be same as driver name */ |
| 675 | pyra_class = class_create(THIS_MODULE, "pyra"); |
| 676 | if (IS_ERR(pyra_class)) |
| 677 | return PTR_ERR(pyra_class); |
| 678 | pyra_class->dev_attrs = pyra_attributes; |
| 679 | pyra_class->dev_bin_attrs = pyra_bin_attributes; |
| 680 | |
| 681 | retval = hid_register_driver(&pyra_driver); |
| 682 | if (retval) |
| 683 | class_destroy(pyra_class); |
| 684 | return retval; |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | static void __exit pyra_exit(void) |
| 688 | { |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 689 | hid_unregister_driver(&pyra_driver); |
Stefan Achatz | 74b643d | 2011-01-30 13:38:27 +0100 | [diff] [blame] | 690 | class_destroy(pyra_class); |
Stefan Achatz | cb7cf3d | 2010-08-29 12:30:18 +0200 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | module_init(pyra_init); |
| 694 | module_exit(pyra_exit); |
| 695 | |
| 696 | MODULE_AUTHOR("Stefan Achatz"); |
| 697 | MODULE_DESCRIPTION("USB Roccat Pyra driver"); |
| 698 | MODULE_LICENSE("GPL v2"); |