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