Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Roccat Kone 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 Kone is a gamer mouse which consists of a mouse part and a keyboard |
| 16 | * part. The keyboard part enables the mouse to execute stored macros with mixed |
| 17 | * key- and button-events. |
| 18 | * |
| 19 | * TODO implement on-the-fly polling-rate change |
| 20 | * The windows driver has the ability to change the polling rate of the |
| 21 | * device on the press of a mousebutton. |
| 22 | * Is it possible to remove and reinstall the urb in raw-event- or any |
| 23 | * other handler, or to defer this action to be executed somewhere else? |
| 24 | * |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 25 | * TODO is it possible to overwrite group for sysfs attributes via udev? |
| 26 | */ |
| 27 | |
| 28 | #include <linux/device.h> |
| 29 | #include <linux/input.h> |
| 30 | #include <linux/hid.h> |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 31 | #include <linux/module.h> |
Tejun Heo | ed28f04 | 2010-03-30 02:52:41 +0900 | [diff] [blame] | 32 | #include <linux/slab.h> |
Stefan Achatz | 5dc0c98 | 2011-02-03 16:14:43 +0100 | [diff] [blame] | 33 | #include <linux/hid-roccat.h> |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 34 | #include "hid-ids.h" |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 35 | #include "hid-roccat-common.h" |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 36 | #include "hid-roccat-kone.h" |
| 37 | |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 38 | static uint profile_numbers[5] = {0, 1, 2, 3, 4}; |
| 39 | |
Stefan Achatz | bd9c35d | 2011-08-27 15:24:48 +0200 | [diff] [blame] | 40 | static void kone_profile_activated(struct kone_device *kone, uint new_profile) |
| 41 | { |
| 42 | kone->actual_profile = new_profile; |
| 43 | kone->actual_dpi = kone->profiles[new_profile - 1].startup_dpi; |
| 44 | } |
| 45 | |
Stefan Achatz | 3200a6a | 2011-08-27 15:24:51 +0200 | [diff] [blame] | 46 | static void kone_profile_report(struct kone_device *kone, uint new_profile) |
| 47 | { |
| 48 | struct kone_roccat_report roccat_report; |
Hans Duedal | 6354b7e | 2014-10-11 17:01:49 +0200 | [diff] [blame] | 49 | |
Stefan Achatz | 3200a6a | 2011-08-27 15:24:51 +0200 | [diff] [blame] | 50 | roccat_report.event = kone_mouse_event_switch_profile; |
| 51 | roccat_report.value = new_profile; |
| 52 | roccat_report.key = 0; |
| 53 | roccat_report_event(kone->chrdev_minor, (uint8_t *)&roccat_report); |
| 54 | } |
| 55 | |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 56 | static int kone_receive(struct usb_device *usb_dev, uint usb_command, |
| 57 | void *data, uint size) |
| 58 | { |
| 59 | char *buf; |
| 60 | int len; |
| 61 | |
| 62 | buf = kmalloc(size, GFP_KERNEL); |
| 63 | if (buf == NULL) |
| 64 | return -ENOMEM; |
| 65 | |
| 66 | len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), |
| 67 | HID_REQ_GET_REPORT, |
| 68 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, |
| 69 | usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT); |
| 70 | |
| 71 | memcpy(data, buf, size); |
| 72 | kfree(buf); |
| 73 | return ((len < 0) ? len : ((len != size) ? -EIO : 0)); |
| 74 | } |
| 75 | |
| 76 | static int kone_send(struct usb_device *usb_dev, uint usb_command, |
| 77 | void const *data, uint size) |
| 78 | { |
| 79 | char *buf; |
| 80 | int len; |
| 81 | |
Thomas Meyer | 4c33a88 | 2011-11-17 23:43:40 +0100 | [diff] [blame] | 82 | buf = kmemdup(data, size, GFP_KERNEL); |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 83 | if (buf == NULL) |
| 84 | return -ENOMEM; |
| 85 | |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 86 | len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), |
| 87 | HID_REQ_SET_REPORT, |
| 88 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, |
| 89 | usb_command, 0, buf, size, USB_CTRL_SET_TIMEOUT); |
| 90 | |
| 91 | kfree(buf); |
| 92 | return ((len < 0) ? len : ((len != size) ? -EIO : 0)); |
| 93 | } |
| 94 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 95 | /* kone_class is used for creating sysfs attributes via roccat char device */ |
| 96 | static struct class *kone_class; |
| 97 | |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 98 | static void kone_set_settings_checksum(struct kone_settings *settings) |
| 99 | { |
| 100 | uint16_t checksum = 0; |
| 101 | unsigned char *address = (unsigned char *)settings; |
| 102 | int i; |
| 103 | |
| 104 | for (i = 0; i < sizeof(struct kone_settings) - 2; ++i, ++address) |
| 105 | checksum += *address; |
| 106 | settings->checksum = cpu_to_le16(checksum); |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Checks success after writing data to mouse |
| 111 | * On success returns 0 |
| 112 | * On failure returns errno |
| 113 | */ |
| 114 | static int kone_check_write(struct usb_device *usb_dev) |
| 115 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 116 | int retval; |
| 117 | uint8_t data; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 118 | |
| 119 | do { |
| 120 | /* |
| 121 | * Mouse needs 50 msecs until it says ok, but there are |
| 122 | * 30 more msecs needed for next write to work. |
| 123 | */ |
| 124 | msleep(80); |
| 125 | |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 126 | retval = kone_receive(usb_dev, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 127 | kone_command_confirm_write, &data, 1); |
| 128 | if (retval) |
| 129 | return retval; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 130 | |
| 131 | /* |
| 132 | * value of 3 seems to mean something like |
| 133 | * "not finished yet, but it looks good" |
| 134 | * So check again after a moment. |
| 135 | */ |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 136 | } while (data == 3); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 137 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 138 | if (data == 1) /* everything alright */ |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 139 | return 0; |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 140 | |
| 141 | /* unknown answer */ |
Stefan Achatz | 4ec141a | 2012-05-20 22:45:08 +0200 | [diff] [blame] | 142 | dev_err(&usb_dev->dev, "got retval %d when checking write\n", data); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 143 | return -EIO; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | /* |
| 147 | * Reads settings from mouse and stores it in @buf |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 148 | * On success returns 0 |
| 149 | * On failure returns errno |
| 150 | */ |
| 151 | static int kone_get_settings(struct usb_device *usb_dev, |
| 152 | struct kone_settings *buf) |
| 153 | { |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 154 | return kone_receive(usb_dev, kone_command_settings, buf, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 155 | sizeof(struct kone_settings)); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Writes settings from @buf to mouse |
| 160 | * On success returns 0 |
| 161 | * On failure returns errno |
| 162 | */ |
| 163 | static int kone_set_settings(struct usb_device *usb_dev, |
| 164 | struct kone_settings const *settings) |
| 165 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 166 | int retval; |
Hans Duedal | 6354b7e | 2014-10-11 17:01:49 +0200 | [diff] [blame] | 167 | |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 168 | retval = kone_send(usb_dev, kone_command_settings, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 169 | settings, sizeof(struct kone_settings)); |
| 170 | if (retval) |
| 171 | return retval; |
| 172 | return kone_check_write(usb_dev); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Reads profile data from mouse and stores it in @buf |
| 177 | * @number: profile number to read |
| 178 | * On success returns 0 |
| 179 | * On failure returns errno |
| 180 | */ |
| 181 | static int kone_get_profile(struct usb_device *usb_dev, |
| 182 | struct kone_profile *buf, int number) |
| 183 | { |
| 184 | int len; |
| 185 | |
| 186 | if (number < 1 || number > 5) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0), |
| 190 | USB_REQ_CLEAR_FEATURE, |
| 191 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN, |
| 192 | kone_command_profile, number, buf, |
| 193 | sizeof(struct kone_profile), USB_CTRL_SET_TIMEOUT); |
| 194 | |
| 195 | if (len != sizeof(struct kone_profile)) |
| 196 | return -EIO; |
| 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Writes profile data to mouse. |
| 203 | * @number: profile number to write |
| 204 | * On success returns 0 |
| 205 | * On failure returns errno |
| 206 | */ |
| 207 | static int kone_set_profile(struct usb_device *usb_dev, |
| 208 | struct kone_profile const *profile, int number) |
| 209 | { |
| 210 | int len; |
| 211 | |
| 212 | if (number < 1 || number > 5) |
| 213 | return -EINVAL; |
| 214 | |
| 215 | len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0), |
| 216 | USB_REQ_SET_CONFIGURATION, |
| 217 | USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 218 | kone_command_profile, number, (void *)profile, |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 219 | sizeof(struct kone_profile), |
| 220 | USB_CTRL_SET_TIMEOUT); |
| 221 | |
| 222 | if (len != sizeof(struct kone_profile)) |
| 223 | return len; |
| 224 | |
| 225 | if (kone_check_write(usb_dev)) |
| 226 | return -EIO; |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Reads value of "fast-clip-weight" and stores it in @result |
| 233 | * On success returns 0 |
| 234 | * On failure returns errno |
| 235 | */ |
| 236 | static int kone_get_weight(struct usb_device *usb_dev, int *result) |
| 237 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 238 | int retval; |
| 239 | uint8_t data; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 240 | |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 241 | retval = kone_receive(usb_dev, kone_command_weight, &data, 1); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 242 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 243 | if (retval) |
| 244 | return retval; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 245 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 246 | *result = (int)data; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | /* |
| 251 | * Reads firmware_version of mouse and stores it in @result |
| 252 | * On success returns 0 |
| 253 | * On failure returns errno |
| 254 | */ |
| 255 | static int kone_get_firmware_version(struct usb_device *usb_dev, int *result) |
| 256 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 257 | int retval; |
| 258 | uint16_t data; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 259 | |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 260 | retval = kone_receive(usb_dev, kone_command_firmware_version, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 261 | &data, 2); |
| 262 | if (retval) |
| 263 | return retval; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 264 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 265 | *result = le16_to_cpu(data); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
Stephen Rothwell | 5f27762 | 2010-05-21 16:15:32 +1000 | [diff] [blame] | 269 | static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj, |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 270 | struct bin_attribute *attr, char *buf, |
| 271 | loff_t off, size_t count) { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 272 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 273 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); |
| 274 | |
| 275 | if (off >= sizeof(struct kone_settings)) |
| 276 | return 0; |
| 277 | |
| 278 | if (off + count > sizeof(struct kone_settings)) |
| 279 | count = sizeof(struct kone_settings) - off; |
| 280 | |
| 281 | mutex_lock(&kone->kone_lock); |
Stefan Achatz | cab6b16 | 2010-06-20 19:19:06 +0200 | [diff] [blame] | 282 | memcpy(buf, ((char const *)&kone->settings) + off, count); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 283 | mutex_unlock(&kone->kone_lock); |
| 284 | |
| 285 | return count; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Writing settings automatically activates startup_profile. |
| 290 | * This function keeps values in kone_device up to date and assumes that in |
| 291 | * case of error the old data is still valid |
| 292 | */ |
Stephen Rothwell | 5f27762 | 2010-05-21 16:15:32 +1000 | [diff] [blame] | 293 | static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj, |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 294 | struct bin_attribute *attr, char *buf, |
| 295 | loff_t off, size_t count) { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 296 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 297 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); |
| 298 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
Stefan Achatz | 3200a6a | 2011-08-27 15:24:51 +0200 | [diff] [blame] | 299 | int retval = 0, difference, old_profile; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 300 | |
| 301 | /* I need to get my data in one piece */ |
| 302 | if (off != 0 || count != sizeof(struct kone_settings)) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | mutex_lock(&kone->kone_lock); |
| 306 | difference = memcmp(buf, &kone->settings, sizeof(struct kone_settings)); |
| 307 | if (difference) { |
| 308 | retval = kone_set_settings(usb_dev, |
| 309 | (struct kone_settings const *)buf); |
Stefan Achatz | bd9c35d | 2011-08-27 15:24:48 +0200 | [diff] [blame] | 310 | if (retval) { |
| 311 | mutex_unlock(&kone->kone_lock); |
| 312 | return retval; |
| 313 | } |
| 314 | |
Stefan Achatz | 3200a6a | 2011-08-27 15:24:51 +0200 | [diff] [blame] | 315 | old_profile = kone->settings.startup_profile; |
Stefan Achatz | bd9c35d | 2011-08-27 15:24:48 +0200 | [diff] [blame] | 316 | memcpy(&kone->settings, buf, sizeof(struct kone_settings)); |
| 317 | |
| 318 | kone_profile_activated(kone, kone->settings.startup_profile); |
Stefan Achatz | 3200a6a | 2011-08-27 15:24:51 +0200 | [diff] [blame] | 319 | |
| 320 | if (kone->settings.startup_profile != old_profile) |
| 321 | kone_profile_report(kone, kone->settings.startup_profile); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 322 | } |
| 323 | mutex_unlock(&kone->kone_lock); |
| 324 | |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 325 | return sizeof(struct kone_settings); |
| 326 | } |
Greg Kroah-Hartman | 71b230a | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 327 | static BIN_ATTR(settings, 0660, kone_sysfs_read_settings, |
| 328 | kone_sysfs_write_settings, sizeof(struct kone_settings)); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 329 | |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 330 | static ssize_t kone_sysfs_read_profilex(struct file *fp, |
| 331 | struct kobject *kobj, struct bin_attribute *attr, |
| 332 | char *buf, loff_t off, size_t count) { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 333 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 334 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); |
| 335 | |
| 336 | if (off >= sizeof(struct kone_profile)) |
| 337 | return 0; |
| 338 | |
| 339 | if (off + count > sizeof(struct kone_profile)) |
| 340 | count = sizeof(struct kone_profile) - off; |
| 341 | |
| 342 | mutex_lock(&kone->kone_lock); |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 343 | memcpy(buf, ((char const *)&kone->profiles[*(uint *)(attr->private)]) + off, count); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 344 | mutex_unlock(&kone->kone_lock); |
| 345 | |
| 346 | return count; |
| 347 | } |
| 348 | |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 349 | /* Writes data only if different to stored data */ |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 350 | static ssize_t kone_sysfs_write_profilex(struct file *fp, |
| 351 | struct kobject *kobj, struct bin_attribute *attr, |
| 352 | char *buf, loff_t off, size_t count) { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 353 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 354 | struct kone_device *kone = hid_get_drvdata(dev_get_drvdata(dev)); |
| 355 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 356 | struct kone_profile *profile; |
| 357 | int retval = 0, difference; |
| 358 | |
| 359 | /* I need to get my data in one piece */ |
| 360 | if (off != 0 || count != sizeof(struct kone_profile)) |
| 361 | return -EINVAL; |
| 362 | |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 363 | profile = &kone->profiles[*(uint *)(attr->private)]; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 364 | |
| 365 | mutex_lock(&kone->kone_lock); |
| 366 | difference = memcmp(buf, profile, sizeof(struct kone_profile)); |
| 367 | if (difference) { |
| 368 | retval = kone_set_profile(usb_dev, |
Stefan Achatz | 14a057f | 2010-11-26 19:57:38 +0000 | [diff] [blame] | 369 | (struct kone_profile const *)buf, |
| 370 | *(uint *)(attr->private) + 1); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 371 | if (!retval) |
| 372 | memcpy(profile, buf, sizeof(struct kone_profile)); |
| 373 | } |
| 374 | mutex_unlock(&kone->kone_lock); |
| 375 | |
| 376 | if (retval) |
| 377 | return retval; |
| 378 | |
| 379 | return sizeof(struct kone_profile); |
| 380 | } |
Greg Kroah-Hartman | 71b230a | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 381 | #define PROFILE_ATTR(number) \ |
| 382 | static struct bin_attribute bin_attr_profile##number = { \ |
Stefan Achatz | 550dbf4 | 2013-09-28 05:57:46 +0200 | [diff] [blame] | 383 | .attr = { .name = "profile" #number, .mode = 0660 }, \ |
Greg Kroah-Hartman | 71b230a | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 384 | .size = sizeof(struct kone_profile), \ |
| 385 | .read = kone_sysfs_read_profilex, \ |
| 386 | .write = kone_sysfs_write_profilex, \ |
Greg Kroah-Hartman | 515ad4d | 2013-08-19 21:56:46 -0700 | [diff] [blame] | 387 | .private = &profile_numbers[number-1], \ |
Hans Duedal | 6354b7e | 2014-10-11 17:01:49 +0200 | [diff] [blame] | 388 | } |
Greg Kroah-Hartman | 71b230a | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 389 | PROFILE_ATTR(1); |
| 390 | PROFILE_ATTR(2); |
| 391 | PROFILE_ATTR(3); |
| 392 | PROFILE_ATTR(4); |
| 393 | PROFILE_ATTR(5); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 394 | |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 395 | static ssize_t kone_sysfs_show_actual_profile(struct device *dev, |
| 396 | struct device_attribute *attr, char *buf) |
| 397 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 398 | struct kone_device *kone = |
| 399 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 400 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_profile); |
| 401 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 402 | static DEVICE_ATTR(actual_profile, 0440, kone_sysfs_show_actual_profile, NULL); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 403 | |
| 404 | static ssize_t kone_sysfs_show_actual_dpi(struct device *dev, |
| 405 | struct device_attribute *attr, char *buf) |
| 406 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 407 | struct kone_device *kone = |
| 408 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 409 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->actual_dpi); |
| 410 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 411 | static DEVICE_ATTR(actual_dpi, 0440, kone_sysfs_show_actual_dpi, NULL); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 412 | |
| 413 | /* weight is read each time, since we don't get informed when it's changed */ |
| 414 | static ssize_t kone_sysfs_show_weight(struct device *dev, |
| 415 | struct device_attribute *attr, char *buf) |
| 416 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 417 | struct kone_device *kone; |
| 418 | struct usb_device *usb_dev; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 419 | int weight = 0; |
| 420 | int retval; |
| 421 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 422 | dev = dev->parent->parent; |
| 423 | kone = hid_get_drvdata(dev_get_drvdata(dev)); |
| 424 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 425 | |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 426 | mutex_lock(&kone->kone_lock); |
| 427 | retval = kone_get_weight(usb_dev, &weight); |
| 428 | mutex_unlock(&kone->kone_lock); |
| 429 | |
| 430 | if (retval) |
| 431 | return retval; |
| 432 | return snprintf(buf, PAGE_SIZE, "%d\n", weight); |
| 433 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 434 | static DEVICE_ATTR(weight, 0440, kone_sysfs_show_weight, NULL); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 435 | |
| 436 | static ssize_t kone_sysfs_show_firmware_version(struct device *dev, |
| 437 | struct device_attribute *attr, char *buf) |
| 438 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 439 | struct kone_device *kone = |
| 440 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 441 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->firmware_version); |
| 442 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 443 | static DEVICE_ATTR(firmware_version, 0440, kone_sysfs_show_firmware_version, |
| 444 | NULL); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 445 | |
| 446 | static ssize_t kone_sysfs_show_tcu(struct device *dev, |
| 447 | struct device_attribute *attr, char *buf) |
| 448 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 449 | struct kone_device *kone = |
| 450 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 451 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.tcu); |
| 452 | } |
| 453 | |
| 454 | static int kone_tcu_command(struct usb_device *usb_dev, int number) |
| 455 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 456 | unsigned char value; |
Hans Duedal | 6354b7e | 2014-10-11 17:01:49 +0200 | [diff] [blame] | 457 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 458 | value = number; |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 459 | return kone_send(usb_dev, kone_command_calibrate, &value, 1); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | /* |
| 463 | * Calibrating the tcu is the only action that changes settings data inside the |
| 464 | * mouse, so this data needs to be reread |
| 465 | */ |
| 466 | static ssize_t kone_sysfs_set_tcu(struct device *dev, |
| 467 | struct device_attribute *attr, char const *buf, size_t size) |
| 468 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 469 | struct kone_device *kone; |
| 470 | struct usb_device *usb_dev; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 471 | int retval; |
| 472 | unsigned long state; |
| 473 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 474 | dev = dev->parent->parent; |
| 475 | kone = hid_get_drvdata(dev_get_drvdata(dev)); |
| 476 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 477 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 478 | retval = kstrtoul(buf, 10, &state); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 479 | if (retval) |
| 480 | return retval; |
| 481 | |
| 482 | if (state != 0 && state != 1) |
| 483 | return -EINVAL; |
| 484 | |
| 485 | mutex_lock(&kone->kone_lock); |
| 486 | |
| 487 | if (state == 1) { /* state activate */ |
| 488 | retval = kone_tcu_command(usb_dev, 1); |
| 489 | if (retval) |
| 490 | goto exit_unlock; |
| 491 | retval = kone_tcu_command(usb_dev, 2); |
| 492 | if (retval) |
| 493 | goto exit_unlock; |
| 494 | ssleep(5); /* tcu needs this time for calibration */ |
| 495 | retval = kone_tcu_command(usb_dev, 3); |
| 496 | if (retval) |
| 497 | goto exit_unlock; |
| 498 | retval = kone_tcu_command(usb_dev, 0); |
| 499 | if (retval) |
| 500 | goto exit_unlock; |
| 501 | retval = kone_tcu_command(usb_dev, 4); |
| 502 | if (retval) |
| 503 | goto exit_unlock; |
| 504 | /* |
| 505 | * Kone needs this time to settle things. |
| 506 | * Reading settings too early will result in invalid data. |
| 507 | * Roccat's driver waits 1 sec, maybe this time could be |
| 508 | * shortened. |
| 509 | */ |
| 510 | ssleep(1); |
| 511 | } |
| 512 | |
| 513 | /* calibration changes values in settings, so reread */ |
| 514 | retval = kone_get_settings(usb_dev, &kone->settings); |
| 515 | if (retval) |
| 516 | goto exit_no_settings; |
| 517 | |
| 518 | /* only write settings back if activation state is different */ |
| 519 | if (kone->settings.tcu != state) { |
| 520 | kone->settings.tcu = state; |
| 521 | kone_set_settings_checksum(&kone->settings); |
| 522 | |
| 523 | retval = kone_set_settings(usb_dev, &kone->settings); |
| 524 | if (retval) { |
Stefan Achatz | 4ec141a | 2012-05-20 22:45:08 +0200 | [diff] [blame] | 525 | dev_err(&usb_dev->dev, "couldn't set tcu state\n"); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 526 | /* |
| 527 | * try to reread valid settings into buffer overwriting |
| 528 | * first error code |
| 529 | */ |
| 530 | retval = kone_get_settings(usb_dev, &kone->settings); |
| 531 | if (retval) |
| 532 | goto exit_no_settings; |
| 533 | goto exit_unlock; |
| 534 | } |
Stefan Achatz | bd9c35d | 2011-08-27 15:24:48 +0200 | [diff] [blame] | 535 | /* calibration resets profile */ |
| 536 | kone_profile_activated(kone, kone->settings.startup_profile); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | retval = size; |
| 540 | exit_no_settings: |
Stefan Achatz | 4ec141a | 2012-05-20 22:45:08 +0200 | [diff] [blame] | 541 | dev_err(&usb_dev->dev, "couldn't read settings\n"); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 542 | exit_unlock: |
| 543 | mutex_unlock(&kone->kone_lock); |
| 544 | return retval; |
| 545 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 546 | static DEVICE_ATTR(tcu, 0660, kone_sysfs_show_tcu, kone_sysfs_set_tcu); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 547 | |
| 548 | static ssize_t kone_sysfs_show_startup_profile(struct device *dev, |
| 549 | struct device_attribute *attr, char *buf) |
| 550 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 551 | struct kone_device *kone = |
| 552 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 553 | return snprintf(buf, PAGE_SIZE, "%d\n", kone->settings.startup_profile); |
| 554 | } |
| 555 | |
| 556 | static ssize_t kone_sysfs_set_startup_profile(struct device *dev, |
| 557 | struct device_attribute *attr, char const *buf, size_t size) |
| 558 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 559 | struct kone_device *kone; |
| 560 | struct usb_device *usb_dev; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 561 | int retval; |
| 562 | unsigned long new_startup_profile; |
| 563 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 564 | dev = dev->parent->parent; |
| 565 | kone = hid_get_drvdata(dev_get_drvdata(dev)); |
| 566 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 567 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 568 | retval = kstrtoul(buf, 10, &new_startup_profile); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 569 | if (retval) |
| 570 | return retval; |
| 571 | |
| 572 | if (new_startup_profile < 1 || new_startup_profile > 5) |
| 573 | return -EINVAL; |
| 574 | |
| 575 | mutex_lock(&kone->kone_lock); |
| 576 | |
| 577 | kone->settings.startup_profile = new_startup_profile; |
| 578 | kone_set_settings_checksum(&kone->settings); |
| 579 | |
| 580 | retval = kone_set_settings(usb_dev, &kone->settings); |
Stefan Achatz | bd9c35d | 2011-08-27 15:24:48 +0200 | [diff] [blame] | 581 | if (retval) { |
| 582 | mutex_unlock(&kone->kone_lock); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 583 | return retval; |
Stefan Achatz | bd9c35d | 2011-08-27 15:24:48 +0200 | [diff] [blame] | 584 | } |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 585 | |
| 586 | /* changing the startup profile immediately activates this profile */ |
Stefan Achatz | bd9c35d | 2011-08-27 15:24:48 +0200 | [diff] [blame] | 587 | kone_profile_activated(kone, new_startup_profile); |
Stefan Achatz | 3200a6a | 2011-08-27 15:24:51 +0200 | [diff] [blame] | 588 | kone_profile_report(kone, new_startup_profile); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 589 | |
Stefan Achatz | bd9c35d | 2011-08-27 15:24:48 +0200 | [diff] [blame] | 590 | mutex_unlock(&kone->kone_lock); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 591 | return size; |
| 592 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 593 | static DEVICE_ATTR(startup_profile, 0660, kone_sysfs_show_startup_profile, |
| 594 | kone_sysfs_set_startup_profile); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 595 | |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 596 | static struct attribute *kone_attrs[] = { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 597 | /* |
| 598 | * Read actual dpi settings. |
| 599 | * Returns raw value for further processing. Refer to enum |
| 600 | * kone_polling_rates to get real value. |
| 601 | */ |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 602 | &dev_attr_actual_dpi.attr, |
| 603 | &dev_attr_actual_profile.attr, |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 604 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 605 | /* |
| 606 | * The mouse can be equipped with one of four supplied weights from 5 |
| 607 | * to 20 grams which are recognized and its value can be read out. |
| 608 | * This returns the raw value reported by the mouse for easy evaluation |
| 609 | * by software. Refer to enum kone_weights to get corresponding real |
| 610 | * weight. |
| 611 | */ |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 612 | &dev_attr_weight.attr, |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 613 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 614 | /* |
| 615 | * Prints firmware version stored in mouse as integer. |
| 616 | * The raw value reported by the mouse is returned for easy evaluation, |
| 617 | * to get the real version number the decimal point has to be shifted 2 |
| 618 | * positions to the left. E.g. a value of 138 means 1.38. |
| 619 | */ |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 620 | &dev_attr_firmware_version.attr, |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 621 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 622 | /* |
| 623 | * Prints state of Tracking Control Unit as number where 0 = off and |
| 624 | * 1 = on. Writing 0 deactivates tcu and writing 1 calibrates and |
| 625 | * activates the tcu |
| 626 | */ |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 627 | &dev_attr_tcu.attr, |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 628 | |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 629 | /* Prints and takes the number of the profile the mouse starts with */ |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 630 | &dev_attr_startup_profile.attr, |
| 631 | NULL, |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 632 | }; |
| 633 | |
Greg Kroah-Hartman | 71b230a | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 634 | static struct bin_attribute *kone_bin_attributes[] = { |
| 635 | &bin_attr_settings, |
| 636 | &bin_attr_profile1, |
| 637 | &bin_attr_profile2, |
| 638 | &bin_attr_profile3, |
| 639 | &bin_attr_profile4, |
| 640 | &bin_attr_profile5, |
| 641 | NULL, |
| 642 | }; |
| 643 | |
| 644 | static const struct attribute_group kone_group = { |
| 645 | .attrs = kone_attrs, |
| 646 | .bin_attrs = kone_bin_attributes, |
| 647 | }; |
| 648 | |
| 649 | static const struct attribute_group *kone_groups[] = { |
| 650 | &kone_group, |
| 651 | NULL, |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 652 | }; |
| 653 | |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 654 | static int kone_init_kone_device_struct(struct usb_device *usb_dev, |
| 655 | struct kone_device *kone) |
| 656 | { |
| 657 | uint i; |
| 658 | int retval; |
| 659 | |
| 660 | mutex_init(&kone->kone_lock); |
| 661 | |
| 662 | for (i = 0; i < 5; ++i) { |
| 663 | retval = kone_get_profile(usb_dev, &kone->profiles[i], i + 1); |
| 664 | if (retval) |
| 665 | return retval; |
| 666 | } |
| 667 | |
| 668 | retval = kone_get_settings(usb_dev, &kone->settings); |
| 669 | if (retval) |
| 670 | return retval; |
| 671 | |
| 672 | retval = kone_get_firmware_version(usb_dev, &kone->firmware_version); |
| 673 | if (retval) |
| 674 | return retval; |
| 675 | |
Stefan Achatz | bd9c35d | 2011-08-27 15:24:48 +0200 | [diff] [blame] | 676 | kone_profile_activated(kone, kone->settings.startup_profile); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 677 | |
| 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Since IGNORE_MOUSE quirk moved to hid-apple, there is no way to bind only to |
| 683 | * mousepart if usb_hid is compiled into the kernel and kone is compiled as |
| 684 | * module. |
| 685 | * Secial behaviour is bound only to mousepart since only mouseevents contain |
| 686 | * additional notifications. |
| 687 | */ |
| 688 | static int kone_init_specials(struct hid_device *hdev) |
| 689 | { |
| 690 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 691 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 692 | struct kone_device *kone; |
| 693 | int retval; |
| 694 | |
| 695 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 696 | == USB_INTERFACE_PROTOCOL_MOUSE) { |
| 697 | |
| 698 | kone = kzalloc(sizeof(*kone), GFP_KERNEL); |
Hans Duedal | 6354b7e | 2014-10-11 17:01:49 +0200 | [diff] [blame] | 699 | if (!kone) |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 700 | return -ENOMEM; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 701 | hid_set_drvdata(hdev, kone); |
| 702 | |
| 703 | retval = kone_init_kone_device_struct(usb_dev, kone); |
| 704 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 705 | hid_err(hdev, "couldn't init struct kone_device\n"); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 706 | goto exit_free; |
| 707 | } |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 708 | |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 709 | retval = roccat_connect(kone_class, hdev, |
| 710 | sizeof(struct kone_roccat_report)); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 711 | if (retval < 0) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 712 | hid_err(hdev, "couldn't init char dev\n"); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 713 | /* be tolerant about not getting chrdev */ |
| 714 | } else { |
| 715 | kone->roccat_claimed = 1; |
| 716 | kone->chrdev_minor = retval; |
| 717 | } |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 718 | } else { |
| 719 | hid_set_drvdata(hdev, NULL); |
| 720 | } |
| 721 | |
| 722 | return 0; |
| 723 | exit_free: |
| 724 | kfree(kone); |
| 725 | return retval; |
| 726 | } |
| 727 | |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 728 | static void kone_remove_specials(struct hid_device *hdev) |
| 729 | { |
| 730 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 731 | struct kone_device *kone; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 732 | |
| 733 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 734 | == USB_INTERFACE_PROTOCOL_MOUSE) { |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 735 | kone = hid_get_drvdata(hdev); |
| 736 | if (kone->roccat_claimed) |
| 737 | roccat_disconnect(kone->chrdev_minor); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 738 | kfree(hid_get_drvdata(hdev)); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | static int kone_probe(struct hid_device *hdev, const struct hid_device_id *id) |
| 743 | { |
| 744 | int retval; |
| 745 | |
| 746 | retval = hid_parse(hdev); |
| 747 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 748 | hid_err(hdev, "parse failed\n"); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 749 | goto exit; |
| 750 | } |
| 751 | |
| 752 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 753 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 754 | hid_err(hdev, "hw start failed\n"); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 755 | goto exit; |
| 756 | } |
| 757 | |
| 758 | retval = kone_init_specials(hdev); |
| 759 | if (retval) { |
Joe Perches | 4291ee3 | 2010-12-09 19:29:03 -0800 | [diff] [blame] | 760 | hid_err(hdev, "couldn't install mouse\n"); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 761 | goto exit_stop; |
| 762 | } |
| 763 | |
| 764 | return 0; |
| 765 | |
| 766 | exit_stop: |
| 767 | hid_hw_stop(hdev); |
| 768 | exit: |
| 769 | return retval; |
| 770 | } |
| 771 | |
| 772 | static void kone_remove(struct hid_device *hdev) |
| 773 | { |
| 774 | kone_remove_specials(hdev); |
| 775 | hid_hw_stop(hdev); |
| 776 | } |
| 777 | |
Stefan Achatz | 48e7080 | 2010-05-18 18:31:04 +0200 | [diff] [blame] | 778 | /* handle special events and keep actual profile and dpi values up to date */ |
| 779 | static void kone_keep_values_up_to_date(struct kone_device *kone, |
| 780 | struct kone_mouse_event const *event) |
| 781 | { |
| 782 | switch (event->event) { |
| 783 | case kone_mouse_event_switch_profile: |
Stefan Achatz | 1c5784d | 2011-08-27 15:24:36 +0200 | [diff] [blame] | 784 | kone->actual_dpi = kone->profiles[event->value - 1]. |
| 785 | startup_dpi; |
Stefan Achatz | 48e7080 | 2010-05-18 18:31:04 +0200 | [diff] [blame] | 786 | case kone_mouse_event_osd_profile: |
| 787 | kone->actual_profile = event->value; |
Stefan Achatz | 48e7080 | 2010-05-18 18:31:04 +0200 | [diff] [blame] | 788 | break; |
| 789 | case kone_mouse_event_switch_dpi: |
| 790 | case kone_mouse_event_osd_dpi: |
| 791 | kone->actual_dpi = event->value; |
| 792 | break; |
| 793 | } |
| 794 | } |
| 795 | |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 796 | static void kone_report_to_chrdev(struct kone_device const *kone, |
| 797 | struct kone_mouse_event const *event) |
| 798 | { |
| 799 | struct kone_roccat_report roccat_report; |
| 800 | |
| 801 | switch (event->event) { |
| 802 | case kone_mouse_event_switch_profile: |
| 803 | case kone_mouse_event_switch_dpi: |
| 804 | case kone_mouse_event_osd_profile: |
| 805 | case kone_mouse_event_osd_dpi: |
| 806 | roccat_report.event = event->event; |
| 807 | roccat_report.value = event->value; |
| 808 | roccat_report.key = 0; |
| 809 | roccat_report_event(kone->chrdev_minor, |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 810 | (uint8_t *)&roccat_report); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 811 | break; |
| 812 | case kone_mouse_event_call_overlong_macro: |
Stefan Achatz | b42065f | 2013-04-07 07:08:44 +0200 | [diff] [blame] | 813 | case kone_mouse_event_multimedia: |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 814 | if (event->value == kone_keystroke_action_press) { |
Stefan Achatz | b42065f | 2013-04-07 07:08:44 +0200 | [diff] [blame] | 815 | roccat_report.event = event->event; |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 816 | roccat_report.value = kone->actual_profile; |
| 817 | roccat_report.key = event->macro_key; |
| 818 | roccat_report_event(kone->chrdev_minor, |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 819 | (uint8_t *)&roccat_report); |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 820 | } |
| 821 | break; |
| 822 | } |
| 823 | |
| 824 | } |
| 825 | |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 826 | /* |
| 827 | * Is called for keyboard- and mousepart. |
| 828 | * Only mousepart gets informations about special events in its extended event |
| 829 | * structure. |
| 830 | */ |
| 831 | static int kone_raw_event(struct hid_device *hdev, struct hid_report *report, |
| 832 | u8 *data, int size) |
| 833 | { |
| 834 | struct kone_device *kone = hid_get_drvdata(hdev); |
| 835 | struct kone_mouse_event *event = (struct kone_mouse_event *)data; |
| 836 | |
| 837 | /* keyboard events are always processed by default handler */ |
| 838 | if (size != sizeof(struct kone_mouse_event)) |
| 839 | return 0; |
| 840 | |
Stefan Achatz | 901e64d | 2011-06-12 10:02:44 +0200 | [diff] [blame] | 841 | if (kone == NULL) |
| 842 | return 0; |
| 843 | |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 844 | /* |
Stefan Achatz | 73b3577 | 2010-05-19 13:53:22 +0200 | [diff] [blame] | 845 | * Firmware 1.38 introduced new behaviour for tilt and special buttons. |
| 846 | * Pressed button is reported in each movement event. |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 847 | * Workaround sends only one event per press. |
| 848 | */ |
Stefan Achatz | 73b3577 | 2010-05-19 13:53:22 +0200 | [diff] [blame] | 849 | if (memcmp(&kone->last_mouse_event.tilt, &event->tilt, 5)) |
| 850 | memcpy(&kone->last_mouse_event, event, |
| 851 | sizeof(struct kone_mouse_event)); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 852 | else |
Stefan Achatz | 73b3577 | 2010-05-19 13:53:22 +0200 | [diff] [blame] | 853 | memset(&event->tilt, 0, 5); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 854 | |
Stefan Achatz | 48e7080 | 2010-05-18 18:31:04 +0200 | [diff] [blame] | 855 | kone_keep_values_up_to_date(kone, event); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 856 | |
Stefan Achatz | 206f5f2 | 2010-05-19 18:55:16 +0200 | [diff] [blame] | 857 | if (kone->roccat_claimed) |
| 858 | kone_report_to_chrdev(kone, event); |
| 859 | |
Stefan Achatz | 48e7080 | 2010-05-18 18:31:04 +0200 | [diff] [blame] | 860 | return 0; /* always do further processing */ |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | static const struct hid_device_id kone_devices[] = { |
| 864 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONE) }, |
| 865 | { } |
| 866 | }; |
| 867 | |
| 868 | MODULE_DEVICE_TABLE(hid, kone_devices); |
| 869 | |
| 870 | static struct hid_driver kone_driver = { |
| 871 | .name = "kone", |
| 872 | .id_table = kone_devices, |
| 873 | .probe = kone_probe, |
| 874 | .remove = kone_remove, |
| 875 | .raw_event = kone_raw_event |
| 876 | }; |
| 877 | |
Stefan Achatz | 00237bc | 2010-05-08 17:20:38 +0200 | [diff] [blame] | 878 | static int __init kone_init(void) |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 879 | { |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 880 | int retval; |
| 881 | |
| 882 | /* class name has to be same as driver name */ |
| 883 | kone_class = class_create(THIS_MODULE, "kone"); |
| 884 | if (IS_ERR(kone_class)) |
| 885 | return PTR_ERR(kone_class); |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 886 | kone_class->dev_groups = kone_groups; |
Stefan Achatz | 5012aad | 2010-11-26 19:57:33 +0000 | [diff] [blame] | 887 | |
| 888 | retval = hid_register_driver(&kone_driver); |
| 889 | if (retval) |
| 890 | class_destroy(kone_class); |
| 891 | return retval; |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 892 | } |
| 893 | |
Stefan Achatz | 00237bc | 2010-05-08 17:20:38 +0200 | [diff] [blame] | 894 | static void __exit kone_exit(void) |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 895 | { |
| 896 | hid_unregister_driver(&kone_driver); |
Stefan Achatz | 74b643da | 2011-01-30 13:38:27 +0100 | [diff] [blame] | 897 | class_destroy(kone_class); |
Stefan Achatz | 14bf62c | 2010-03-18 16:19:43 +0100 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | module_init(kone_init); |
| 901 | module_exit(kone_exit); |
| 902 | |
Stefan Achatz | 1f749d8 | 2010-05-12 17:43:34 +0200 | [diff] [blame] | 903 | MODULE_AUTHOR("Stefan Achatz"); |
| 904 | MODULE_DESCRIPTION("USB Roccat Kone driver"); |
| 905 | MODULE_LICENSE("GPL v2"); |