Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Roccat Kova[+] driver for Linux |
| 3 | * |
| 4 | * Copyright (c) 2011 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 Kova[+] is a bigger version of the Pyra with two more side buttons. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <linux/hid.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
Stefan Achatz | 5dc0c98 | 2011-02-03 16:14:43 +0100 | [diff] [blame] | 23 | #include <linux/hid-roccat.h> |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 24 | #include "hid-ids.h" |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 25 | #include "hid-roccat-common.h" |
| 26 | #include "hid-roccat-kovaplus.h" |
| 27 | |
| 28 | static uint profile_numbers[5] = {0, 1, 2, 3, 4}; |
| 29 | |
| 30 | static struct class *kovaplus_class; |
| 31 | |
| 32 | static uint kovaplus_convert_event_cpi(uint value) |
| 33 | { |
| 34 | return (value == 7 ? 4 : (value == 4 ? 3 : value)); |
| 35 | } |
| 36 | |
| 37 | static void kovaplus_profile_activated(struct kovaplus_device *kovaplus, |
| 38 | uint new_profile_index) |
| 39 | { |
| 40 | kovaplus->actual_profile = new_profile_index; |
| 41 | kovaplus->actual_cpi = kovaplus->profile_settings[new_profile_index].cpi_startup_level; |
| 42 | kovaplus->actual_x_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_x; |
| 43 | kovaplus->actual_y_sensitivity = kovaplus->profile_settings[new_profile_index].sensitivity_y; |
| 44 | } |
| 45 | |
| 46 | static int kovaplus_send_control(struct usb_device *usb_dev, uint value, |
| 47 | enum kovaplus_control_requests request) |
| 48 | { |
| 49 | int retval; |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 50 | struct roccat_common2_control control; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 51 | |
| 52 | if ((request == KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS || |
| 53 | request == KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) && |
| 54 | value > 4) |
| 55 | return -EINVAL; |
| 56 | |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 57 | control.command = ROCCAT_COMMON_COMMAND_CONTROL; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 58 | control.value = value; |
| 59 | control.request = request; |
| 60 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 61 | retval = roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL, |
| 62 | &control, sizeof(struct roccat_common2_control)); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 63 | |
| 64 | return retval; |
| 65 | } |
| 66 | |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 67 | static int kovaplus_select_profile(struct usb_device *usb_dev, uint number, |
| 68 | enum kovaplus_control_requests request) |
| 69 | { |
| 70 | return kovaplus_send_control(usb_dev, number, request); |
| 71 | } |
| 72 | |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 73 | static int kovaplus_get_profile_settings(struct usb_device *usb_dev, |
| 74 | struct kovaplus_profile_settings *buf, uint number) |
| 75 | { |
| 76 | int retval; |
| 77 | |
| 78 | retval = kovaplus_select_profile(usb_dev, number, |
| 79 | KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS); |
| 80 | if (retval) |
| 81 | return retval; |
| 82 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 83 | return roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_PROFILE_SETTINGS, |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 84 | buf, KOVAPLUS_SIZE_PROFILE_SETTINGS); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | static int kovaplus_get_profile_buttons(struct usb_device *usb_dev, |
| 88 | struct kovaplus_profile_buttons *buf, int number) |
| 89 | { |
| 90 | int retval; |
| 91 | |
| 92 | retval = kovaplus_select_profile(usb_dev, number, |
| 93 | KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS); |
| 94 | if (retval) |
| 95 | return retval; |
| 96 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 97 | return roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_PROFILE_BUTTONS, |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 98 | buf, KOVAPLUS_SIZE_PROFILE_BUTTONS); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | /* retval is 0-4 on success, < 0 on error */ |
| 102 | static int kovaplus_get_actual_profile(struct usb_device *usb_dev) |
| 103 | { |
| 104 | struct kovaplus_actual_profile buf; |
| 105 | int retval; |
| 106 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 107 | retval = roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_ACTUAL_PROFILE, |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 108 | &buf, sizeof(struct kovaplus_actual_profile)); |
| 109 | |
| 110 | return retval ? retval : buf.actual_profile; |
| 111 | } |
| 112 | |
| 113 | static int kovaplus_set_actual_profile(struct usb_device *usb_dev, |
| 114 | int new_profile) |
| 115 | { |
| 116 | struct kovaplus_actual_profile buf; |
| 117 | |
| 118 | buf.command = KOVAPLUS_COMMAND_ACTUAL_PROFILE; |
| 119 | buf.size = sizeof(struct kovaplus_actual_profile); |
| 120 | buf.actual_profile = new_profile; |
| 121 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 122 | return roccat_common2_send_with_status(usb_dev, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 123 | KOVAPLUS_COMMAND_ACTUAL_PROFILE, |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 124 | &buf, sizeof(struct kovaplus_actual_profile)); |
| 125 | } |
| 126 | |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 127 | static ssize_t kovaplus_sysfs_read(struct file *fp, struct kobject *kobj, |
| 128 | char *buf, loff_t off, size_t count, |
| 129 | size_t real_size, uint command) |
| 130 | { |
| 131 | struct device *dev = |
| 132 | container_of(kobj, struct device, kobj)->parent->parent; |
| 133 | struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev)); |
| 134 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 135 | int retval; |
| 136 | |
| 137 | if (off >= real_size) |
| 138 | return 0; |
| 139 | |
| 140 | if (off != 0 || count != real_size) |
| 141 | return -EINVAL; |
| 142 | |
| 143 | mutex_lock(&kovaplus->kovaplus_lock); |
| 144 | retval = roccat_common2_receive(usb_dev, command, buf, real_size); |
| 145 | mutex_unlock(&kovaplus->kovaplus_lock); |
| 146 | |
| 147 | if (retval) |
| 148 | return retval; |
| 149 | |
| 150 | return real_size; |
| 151 | } |
| 152 | |
| 153 | static ssize_t kovaplus_sysfs_write(struct file *fp, struct kobject *kobj, |
| 154 | void const *buf, loff_t off, size_t count, |
| 155 | size_t real_size, uint command) |
| 156 | { |
| 157 | struct device *dev = |
| 158 | container_of(kobj, struct device, kobj)->parent->parent; |
| 159 | struct kovaplus_device *kovaplus = hid_get_drvdata(dev_get_drvdata(dev)); |
| 160 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 161 | int retval; |
| 162 | |
| 163 | if (off != 0 || count != real_size) |
| 164 | return -EINVAL; |
| 165 | |
| 166 | mutex_lock(&kovaplus->kovaplus_lock); |
| 167 | retval = roccat_common2_send_with_status(usb_dev, command, |
| 168 | buf, real_size); |
| 169 | mutex_unlock(&kovaplus->kovaplus_lock); |
| 170 | |
| 171 | if (retval) |
| 172 | return retval; |
| 173 | |
| 174 | return real_size; |
| 175 | } |
| 176 | |
| 177 | #define KOVAPLUS_SYSFS_W(thingy, THINGY) \ |
| 178 | static ssize_t kovaplus_sysfs_write_ ## thingy(struct file *fp, \ |
| 179 | struct kobject *kobj, struct bin_attribute *attr, char *buf, \ |
| 180 | loff_t off, size_t count) \ |
| 181 | { \ |
| 182 | return kovaplus_sysfs_write(fp, kobj, buf, off, count, \ |
| 183 | KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \ |
| 184 | } |
| 185 | |
| 186 | #define KOVAPLUS_SYSFS_R(thingy, THINGY) \ |
| 187 | static ssize_t kovaplus_sysfs_read_ ## thingy(struct file *fp, \ |
| 188 | struct kobject *kobj, struct bin_attribute *attr, char *buf, \ |
| 189 | loff_t off, size_t count) \ |
| 190 | { \ |
| 191 | return kovaplus_sysfs_read(fp, kobj, buf, off, count, \ |
| 192 | KOVAPLUS_SIZE_ ## THINGY, KOVAPLUS_COMMAND_ ## THINGY); \ |
| 193 | } |
| 194 | |
| 195 | #define KOVAPLUS_SYSFS_RW(thingy, THINGY) \ |
| 196 | KOVAPLUS_SYSFS_W(thingy, THINGY) \ |
| 197 | KOVAPLUS_SYSFS_R(thingy, THINGY) |
| 198 | |
| 199 | #define KOVAPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \ |
Greg Kroah-Hartman | 975b53c | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 200 | KOVAPLUS_SYSFS_RW(thingy, THINGY); \ |
| 201 | static struct bin_attribute bin_attr_##thingy = { \ |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 202 | .attr = { .name = #thingy, .mode = 0660 }, \ |
| 203 | .size = KOVAPLUS_SIZE_ ## THINGY, \ |
| 204 | .read = kovaplus_sysfs_read_ ## thingy, \ |
| 205 | .write = kovaplus_sysfs_write_ ## thingy \ |
| 206 | } |
| 207 | |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 208 | #define KOVAPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \ |
Greg Kroah-Hartman | 975b53c | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 209 | KOVAPLUS_SYSFS_W(thingy, THINGY); \ |
| 210 | static struct bin_attribute bin_attr_##thingy = { \ |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 211 | .attr = { .name = #thingy, .mode = 0220 }, \ |
| 212 | .size = KOVAPLUS_SIZE_ ## THINGY, \ |
| 213 | .write = kovaplus_sysfs_write_ ## thingy \ |
| 214 | } |
Greg Kroah-Hartman | 975b53c | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 215 | KOVAPLUS_BIN_ATTRIBUTE_W(control, CONTROL); |
| 216 | KOVAPLUS_BIN_ATTRIBUTE_RW(info, INFO); |
| 217 | KOVAPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS); |
| 218 | KOVAPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS); |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 219 | |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 220 | static ssize_t kovaplus_sysfs_read_profilex_settings(struct file *fp, |
| 221 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 222 | loff_t off, size_t count) |
| 223 | { |
| 224 | struct device *dev = |
| 225 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 226 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 227 | ssize_t retval; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 228 | |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 229 | retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private), |
| 230 | KOVAPLUS_CONTROL_REQUEST_PROFILE_SETTINGS); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 231 | if (retval) |
| 232 | return retval; |
| 233 | |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 234 | return kovaplus_sysfs_read(fp, kobj, buf, off, count, |
| 235 | KOVAPLUS_SIZE_PROFILE_SETTINGS, |
| 236 | KOVAPLUS_COMMAND_PROFILE_SETTINGS); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | static ssize_t kovaplus_sysfs_read_profilex_buttons(struct file *fp, |
| 240 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 241 | loff_t off, size_t count) |
| 242 | { |
| 243 | struct device *dev = |
| 244 | container_of(kobj, struct device, kobj)->parent->parent; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 245 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 246 | ssize_t retval; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 247 | |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 248 | retval = kovaplus_select_profile(usb_dev, *(uint *)(attr->private), |
| 249 | KOVAPLUS_CONTROL_REQUEST_PROFILE_BUTTONS); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 250 | if (retval) |
| 251 | return retval; |
| 252 | |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 253 | return kovaplus_sysfs_read(fp, kobj, buf, off, count, |
| 254 | KOVAPLUS_SIZE_PROFILE_BUTTONS, |
| 255 | KOVAPLUS_COMMAND_PROFILE_BUTTONS); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Greg Kroah-Hartman | 975b53c | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 258 | #define PROFILE_ATTR(number) \ |
| 259 | static struct bin_attribute bin_attr_profile##number##_settings = { \ |
Stefan Achatz | 550dbf4 | 2013-09-28 05:57:46 +0200 | [diff] [blame] | 260 | .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \ |
Greg Kroah-Hartman | 975b53c | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 261 | .size = KOVAPLUS_SIZE_PROFILE_SETTINGS, \ |
| 262 | .read = kovaplus_sysfs_read_profilex_settings, \ |
| 263 | .private = &profile_numbers[number-1], \ |
| 264 | }; \ |
| 265 | static struct bin_attribute bin_attr_profile##number##_buttons = { \ |
Stefan Achatz | 550dbf4 | 2013-09-28 05:57:46 +0200 | [diff] [blame] | 266 | .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \ |
Greg Kroah-Hartman | 975b53c | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 267 | .size = KOVAPLUS_SIZE_PROFILE_BUTTONS, \ |
| 268 | .read = kovaplus_sysfs_read_profilex_buttons, \ |
| 269 | .private = &profile_numbers[number-1], \ |
| 270 | }; |
| 271 | PROFILE_ATTR(1); |
| 272 | PROFILE_ATTR(2); |
| 273 | PROFILE_ATTR(3); |
| 274 | PROFILE_ATTR(4); |
| 275 | PROFILE_ATTR(5); |
| 276 | |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 277 | static ssize_t kovaplus_sysfs_show_actual_profile(struct device *dev, |
| 278 | struct device_attribute *attr, char *buf) |
| 279 | { |
| 280 | struct kovaplus_device *kovaplus = |
| 281 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 282 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_profile); |
| 283 | } |
| 284 | |
| 285 | static ssize_t kovaplus_sysfs_set_actual_profile(struct device *dev, |
| 286 | struct device_attribute *attr, char const *buf, size_t size) |
| 287 | { |
| 288 | struct kovaplus_device *kovaplus; |
| 289 | struct usb_device *usb_dev; |
| 290 | unsigned long profile; |
| 291 | int retval; |
Stefan Achatz | 6b9a57b | 2011-08-27 15:24:45 +0200 | [diff] [blame] | 292 | struct kovaplus_roccat_report roccat_report; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 293 | |
| 294 | dev = dev->parent->parent; |
| 295 | kovaplus = hid_get_drvdata(dev_get_drvdata(dev)); |
| 296 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 297 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 298 | retval = kstrtoul(buf, 10, &profile); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 299 | if (retval) |
| 300 | return retval; |
| 301 | |
| 302 | if (profile >= 5) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | mutex_lock(&kovaplus->kovaplus_lock); |
| 306 | retval = kovaplus_set_actual_profile(usb_dev, profile); |
Stefan Achatz | 6b9a57b | 2011-08-27 15:24:45 +0200 | [diff] [blame] | 307 | if (retval) { |
| 308 | mutex_unlock(&kovaplus->kovaplus_lock); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 309 | return retval; |
Stefan Achatz | 6b9a57b | 2011-08-27 15:24:45 +0200 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | kovaplus_profile_activated(kovaplus, profile); |
| 313 | |
| 314 | roccat_report.type = KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1; |
| 315 | roccat_report.profile = profile + 1; |
| 316 | roccat_report.button = 0; |
| 317 | roccat_report.data1 = profile + 1; |
| 318 | roccat_report.data2 = 0; |
| 319 | roccat_report_event(kovaplus->chrdev_minor, |
| 320 | (uint8_t const *)&roccat_report); |
| 321 | |
| 322 | mutex_unlock(&kovaplus->kovaplus_lock); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 323 | |
| 324 | return size; |
| 325 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 326 | static DEVICE_ATTR(actual_profile, 0660, |
| 327 | kovaplus_sysfs_show_actual_profile, |
| 328 | kovaplus_sysfs_set_actual_profile); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 329 | |
| 330 | static ssize_t kovaplus_sysfs_show_actual_cpi(struct device *dev, |
| 331 | struct device_attribute *attr, char *buf) |
| 332 | { |
| 333 | struct kovaplus_device *kovaplus = |
| 334 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 335 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_cpi); |
| 336 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 337 | static DEVICE_ATTR(actual_cpi, 0440, kovaplus_sysfs_show_actual_cpi, NULL); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 338 | |
| 339 | static ssize_t kovaplus_sysfs_show_actual_sensitivity_x(struct device *dev, |
| 340 | struct device_attribute *attr, char *buf) |
| 341 | { |
| 342 | struct kovaplus_device *kovaplus = |
| 343 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 344 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_x_sensitivity); |
| 345 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 346 | static DEVICE_ATTR(actual_sensitivity_x, 0440, |
| 347 | kovaplus_sysfs_show_actual_sensitivity_x, NULL); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 348 | |
| 349 | static ssize_t kovaplus_sysfs_show_actual_sensitivity_y(struct device *dev, |
| 350 | struct device_attribute *attr, char *buf) |
| 351 | { |
| 352 | struct kovaplus_device *kovaplus = |
| 353 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 354 | return snprintf(buf, PAGE_SIZE, "%d\n", kovaplus->actual_y_sensitivity); |
| 355 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 356 | static DEVICE_ATTR(actual_sensitivity_y, 0440, |
| 357 | kovaplus_sysfs_show_actual_sensitivity_y, NULL); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 358 | |
| 359 | static ssize_t kovaplus_sysfs_show_firmware_version(struct device *dev, |
| 360 | struct device_attribute *attr, char *buf) |
| 361 | { |
Stefan Achatz | 94a8fcf | 2012-11-11 06:20:52 +0100 | [diff] [blame] | 362 | struct kovaplus_device *kovaplus; |
| 363 | struct usb_device *usb_dev; |
| 364 | struct kovaplus_info info; |
| 365 | |
| 366 | dev = dev->parent->parent; |
| 367 | kovaplus = hid_get_drvdata(dev_get_drvdata(dev)); |
| 368 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 369 | |
| 370 | mutex_lock(&kovaplus->kovaplus_lock); |
| 371 | roccat_common2_receive(usb_dev, KOVAPLUS_COMMAND_INFO, |
| 372 | &info, KOVAPLUS_SIZE_INFO); |
| 373 | mutex_unlock(&kovaplus->kovaplus_lock); |
| 374 | |
| 375 | return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 376 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 377 | static DEVICE_ATTR(firmware_version, 0440, |
| 378 | kovaplus_sysfs_show_firmware_version, NULL); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 379 | |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 380 | static struct attribute *kovaplus_attrs[] = { |
| 381 | &dev_attr_actual_cpi.attr, |
| 382 | &dev_attr_firmware_version.attr, |
| 383 | &dev_attr_actual_profile.attr, |
| 384 | &dev_attr_actual_sensitivity_x.attr, |
| 385 | &dev_attr_actual_sensitivity_y.attr, |
| 386 | NULL, |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 387 | }; |
| 388 | |
Greg Kroah-Hartman | 975b53c | 2013-08-19 21:50:28 -0700 | [diff] [blame] | 389 | static struct bin_attribute *kovaplus_bin_attributes[] = { |
| 390 | &bin_attr_control, |
| 391 | &bin_attr_info, |
| 392 | &bin_attr_profile_settings, |
| 393 | &bin_attr_profile_buttons, |
| 394 | &bin_attr_profile1_settings, |
| 395 | &bin_attr_profile2_settings, |
| 396 | &bin_attr_profile3_settings, |
| 397 | &bin_attr_profile4_settings, |
| 398 | &bin_attr_profile5_settings, |
| 399 | &bin_attr_profile1_buttons, |
| 400 | &bin_attr_profile2_buttons, |
| 401 | &bin_attr_profile3_buttons, |
| 402 | &bin_attr_profile4_buttons, |
| 403 | &bin_attr_profile5_buttons, |
| 404 | NULL, |
| 405 | }; |
| 406 | |
| 407 | static const struct attribute_group kovaplus_group = { |
| 408 | .attrs = kovaplus_attrs, |
| 409 | .bin_attrs = kovaplus_bin_attributes, |
| 410 | }; |
| 411 | |
| 412 | static const struct attribute_group *kovaplus_groups[] = { |
| 413 | &kovaplus_group, |
| 414 | NULL, |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 415 | }; |
| 416 | |
| 417 | static int kovaplus_init_kovaplus_device_struct(struct usb_device *usb_dev, |
| 418 | struct kovaplus_device *kovaplus) |
| 419 | { |
| 420 | int retval, i; |
| 421 | static uint wait = 70; /* device will freeze with just 60 */ |
| 422 | |
| 423 | mutex_init(&kovaplus->kovaplus_lock); |
| 424 | |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 425 | for (i = 0; i < 5; ++i) { |
| 426 | msleep(wait); |
| 427 | retval = kovaplus_get_profile_settings(usb_dev, |
| 428 | &kovaplus->profile_settings[i], i); |
| 429 | if (retval) |
| 430 | return retval; |
| 431 | |
| 432 | msleep(wait); |
| 433 | retval = kovaplus_get_profile_buttons(usb_dev, |
| 434 | &kovaplus->profile_buttons[i], i); |
| 435 | if (retval) |
| 436 | return retval; |
| 437 | } |
| 438 | |
| 439 | msleep(wait); |
| 440 | retval = kovaplus_get_actual_profile(usb_dev); |
| 441 | if (retval < 0) |
| 442 | return retval; |
| 443 | kovaplus_profile_activated(kovaplus, retval); |
| 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static int kovaplus_init_specials(struct hid_device *hdev) |
| 449 | { |
| 450 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 451 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 452 | struct kovaplus_device *kovaplus; |
| 453 | int retval; |
| 454 | |
| 455 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 456 | == USB_INTERFACE_PROTOCOL_MOUSE) { |
| 457 | |
| 458 | kovaplus = kzalloc(sizeof(*kovaplus), GFP_KERNEL); |
| 459 | if (!kovaplus) { |
| 460 | hid_err(hdev, "can't alloc device descriptor\n"); |
| 461 | return -ENOMEM; |
| 462 | } |
| 463 | hid_set_drvdata(hdev, kovaplus); |
| 464 | |
| 465 | retval = kovaplus_init_kovaplus_device_struct(usb_dev, kovaplus); |
| 466 | if (retval) { |
| 467 | hid_err(hdev, "couldn't init struct kovaplus_device\n"); |
| 468 | goto exit_free; |
| 469 | } |
| 470 | |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 471 | retval = roccat_connect(kovaplus_class, hdev, |
| 472 | sizeof(struct kovaplus_roccat_report)); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 473 | if (retval < 0) { |
| 474 | hid_err(hdev, "couldn't init char dev\n"); |
| 475 | } else { |
| 476 | kovaplus->chrdev_minor = retval; |
| 477 | kovaplus->roccat_claimed = 1; |
| 478 | } |
| 479 | |
| 480 | } else { |
| 481 | hid_set_drvdata(hdev, NULL); |
| 482 | } |
| 483 | |
| 484 | return 0; |
| 485 | exit_free: |
| 486 | kfree(kovaplus); |
| 487 | return retval; |
| 488 | } |
| 489 | |
| 490 | static void kovaplus_remove_specials(struct hid_device *hdev) |
| 491 | { |
| 492 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 493 | struct kovaplus_device *kovaplus; |
| 494 | |
| 495 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 496 | == USB_INTERFACE_PROTOCOL_MOUSE) { |
| 497 | kovaplus = hid_get_drvdata(hdev); |
| 498 | if (kovaplus->roccat_claimed) |
| 499 | roccat_disconnect(kovaplus->chrdev_minor); |
| 500 | kfree(kovaplus); |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | static int kovaplus_probe(struct hid_device *hdev, |
| 505 | const struct hid_device_id *id) |
| 506 | { |
| 507 | int retval; |
| 508 | |
| 509 | retval = hid_parse(hdev); |
| 510 | if (retval) { |
| 511 | hid_err(hdev, "parse failed\n"); |
| 512 | goto exit; |
| 513 | } |
| 514 | |
| 515 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 516 | if (retval) { |
| 517 | hid_err(hdev, "hw start failed\n"); |
| 518 | goto exit; |
| 519 | } |
| 520 | |
| 521 | retval = kovaplus_init_specials(hdev); |
| 522 | if (retval) { |
| 523 | hid_err(hdev, "couldn't install mouse\n"); |
| 524 | goto exit_stop; |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | |
| 529 | exit_stop: |
| 530 | hid_hw_stop(hdev); |
| 531 | exit: |
| 532 | return retval; |
| 533 | } |
| 534 | |
| 535 | static void kovaplus_remove(struct hid_device *hdev) |
| 536 | { |
| 537 | kovaplus_remove_specials(hdev); |
| 538 | hid_hw_stop(hdev); |
| 539 | } |
| 540 | |
| 541 | static void kovaplus_keep_values_up_to_date(struct kovaplus_device *kovaplus, |
| 542 | u8 const *data) |
| 543 | { |
| 544 | struct kovaplus_mouse_report_button const *button_report; |
| 545 | |
| 546 | if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON) |
| 547 | return; |
| 548 | |
| 549 | button_report = (struct kovaplus_mouse_report_button const *)data; |
| 550 | |
| 551 | switch (button_report->type) { |
| 552 | case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_1: |
| 553 | kovaplus_profile_activated(kovaplus, button_report->data1 - 1); |
| 554 | break; |
| 555 | case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI: |
| 556 | kovaplus->actual_cpi = kovaplus_convert_event_cpi(button_report->data1); |
Stefan Achatz | 7be63f2 | 2013-11-03 06:25:33 +0100 | [diff] [blame] | 557 | break; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 558 | case KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SENSITIVITY: |
| 559 | kovaplus->actual_x_sensitivity = button_report->data1; |
| 560 | kovaplus->actual_y_sensitivity = button_report->data2; |
Stefan Achatz | 7be63f2 | 2013-11-03 06:25:33 +0100 | [diff] [blame] | 561 | break; |
| 562 | default: |
| 563 | break; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 564 | } |
| 565 | } |
| 566 | |
| 567 | static void kovaplus_report_to_chrdev(struct kovaplus_device const *kovaplus, |
| 568 | u8 const *data) |
| 569 | { |
| 570 | struct kovaplus_roccat_report roccat_report; |
| 571 | struct kovaplus_mouse_report_button const *button_report; |
| 572 | |
| 573 | if (data[0] != KOVAPLUS_MOUSE_REPORT_NUMBER_BUTTON) |
| 574 | return; |
| 575 | |
| 576 | button_report = (struct kovaplus_mouse_report_button const *)data; |
| 577 | |
| 578 | if (button_report->type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE_2) |
| 579 | return; |
| 580 | |
| 581 | roccat_report.type = button_report->type; |
| 582 | roccat_report.profile = kovaplus->actual_profile + 1; |
| 583 | |
| 584 | if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_MACRO || |
| 585 | roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_SHORTCUT || |
| 586 | roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH || |
| 587 | roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) |
| 588 | roccat_report.button = button_report->data1; |
| 589 | else |
| 590 | roccat_report.button = 0; |
| 591 | |
| 592 | if (roccat_report.type == KOVAPLUS_MOUSE_REPORT_BUTTON_TYPE_CPI) |
| 593 | roccat_report.data1 = kovaplus_convert_event_cpi(button_report->data1); |
| 594 | else |
| 595 | roccat_report.data1 = button_report->data1; |
| 596 | |
| 597 | roccat_report.data2 = button_report->data2; |
| 598 | |
| 599 | roccat_report_event(kovaplus->chrdev_minor, |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 600 | (uint8_t const *)&roccat_report); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | static int kovaplus_raw_event(struct hid_device *hdev, |
| 604 | struct hid_report *report, u8 *data, int size) |
| 605 | { |
| 606 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 607 | struct kovaplus_device *kovaplus = hid_get_drvdata(hdev); |
| 608 | |
| 609 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 610 | != USB_INTERFACE_PROTOCOL_MOUSE) |
| 611 | return 0; |
| 612 | |
Stefan Achatz | 901e64d | 2011-06-12 10:02:44 +0200 | [diff] [blame] | 613 | if (kovaplus == NULL) |
| 614 | return 0; |
| 615 | |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 616 | kovaplus_keep_values_up_to_date(kovaplus, data); |
| 617 | |
| 618 | if (kovaplus->roccat_claimed) |
| 619 | kovaplus_report_to_chrdev(kovaplus, data); |
| 620 | |
| 621 | return 0; |
| 622 | } |
| 623 | |
| 624 | static const struct hid_device_id kovaplus_devices[] = { |
| 625 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KOVAPLUS) }, |
| 626 | { } |
| 627 | }; |
| 628 | |
| 629 | MODULE_DEVICE_TABLE(hid, kovaplus_devices); |
| 630 | |
| 631 | static struct hid_driver kovaplus_driver = { |
| 632 | .name = "kovaplus", |
| 633 | .id_table = kovaplus_devices, |
| 634 | .probe = kovaplus_probe, |
| 635 | .remove = kovaplus_remove, |
| 636 | .raw_event = kovaplus_raw_event |
| 637 | }; |
| 638 | |
| 639 | static int __init kovaplus_init(void) |
| 640 | { |
| 641 | int retval; |
| 642 | |
| 643 | kovaplus_class = class_create(THIS_MODULE, "kovaplus"); |
| 644 | if (IS_ERR(kovaplus_class)) |
| 645 | return PTR_ERR(kovaplus_class); |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 646 | kovaplus_class->dev_groups = kovaplus_groups; |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 647 | |
| 648 | retval = hid_register_driver(&kovaplus_driver); |
| 649 | if (retval) |
| 650 | class_destroy(kovaplus_class); |
| 651 | return retval; |
| 652 | } |
| 653 | |
| 654 | static void __exit kovaplus_exit(void) |
| 655 | { |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 656 | hid_unregister_driver(&kovaplus_driver); |
Stefan Achatz | 74b643d | 2011-01-30 13:38:27 +0100 | [diff] [blame] | 657 | class_destroy(kovaplus_class); |
Stefan Achatz | 0e70f97 | 2011-01-30 13:38:24 +0100 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | module_init(kovaplus_init); |
| 661 | module_exit(kovaplus_exit); |
| 662 | |
| 663 | MODULE_AUTHOR("Stefan Achatz"); |
| 664 | MODULE_DESCRIPTION("USB Roccat Kova[+] driver"); |
| 665 | MODULE_LICENSE("GPL v2"); |