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