Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Roccat Isku driver for Linux |
| 3 | * |
| 4 | * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net> |
| 5 | */ |
| 6 | |
| 7 | /* |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | */ |
| 13 | |
| 14 | /* |
| 15 | * Roccat Isku is a gamer keyboard with macro keys that can be configured in |
| 16 | * 5 profiles. |
| 17 | */ |
| 18 | |
| 19 | #include <linux/device.h> |
| 20 | #include <linux/input.h> |
| 21 | #include <linux/hid.h> |
| 22 | #include <linux/module.h> |
| 23 | #include <linux/slab.h> |
| 24 | #include <linux/hid-roccat.h> |
| 25 | #include "hid-ids.h" |
| 26 | #include "hid-roccat-common.h" |
| 27 | #include "hid-roccat-isku.h" |
| 28 | |
| 29 | static struct class *isku_class; |
| 30 | |
| 31 | static void isku_profile_activated(struct isku_device *isku, uint new_profile) |
| 32 | { |
| 33 | isku->actual_profile = new_profile; |
| 34 | } |
| 35 | |
| 36 | static int isku_receive(struct usb_device *usb_dev, uint command, |
| 37 | void *buf, uint size) |
| 38 | { |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 39 | return roccat_common2_receive(usb_dev, command, buf, size); |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 42 | static int isku_get_actual_profile(struct usb_device *usb_dev) |
| 43 | { |
| 44 | struct isku_actual_profile buf; |
| 45 | int retval; |
| 46 | |
| 47 | retval = isku_receive(usb_dev, ISKU_COMMAND_ACTUAL_PROFILE, |
| 48 | &buf, sizeof(struct isku_actual_profile)); |
| 49 | return retval ? retval : buf.actual_profile; |
| 50 | } |
| 51 | |
| 52 | static int isku_set_actual_profile(struct usb_device *usb_dev, int new_profile) |
| 53 | { |
| 54 | struct isku_actual_profile buf; |
| 55 | |
| 56 | buf.command = ISKU_COMMAND_ACTUAL_PROFILE; |
| 57 | buf.size = sizeof(struct isku_actual_profile); |
| 58 | buf.actual_profile = new_profile; |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 59 | return roccat_common2_send_with_status(usb_dev, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 60 | ISKU_COMMAND_ACTUAL_PROFILE, &buf, |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 61 | sizeof(struct isku_actual_profile)); |
| 62 | } |
| 63 | |
| 64 | static ssize_t isku_sysfs_show_actual_profile(struct device *dev, |
| 65 | struct device_attribute *attr, char *buf) |
| 66 | { |
| 67 | struct isku_device *isku = |
| 68 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 69 | return snprintf(buf, PAGE_SIZE, "%d\n", isku->actual_profile); |
| 70 | } |
| 71 | |
| 72 | static ssize_t isku_sysfs_set_actual_profile(struct device *dev, |
| 73 | struct device_attribute *attr, char const *buf, size_t size) |
| 74 | { |
| 75 | struct isku_device *isku; |
| 76 | struct usb_device *usb_dev; |
| 77 | unsigned long profile; |
| 78 | int retval; |
| 79 | struct isku_roccat_report roccat_report; |
| 80 | |
| 81 | dev = dev->parent->parent; |
| 82 | isku = hid_get_drvdata(dev_get_drvdata(dev)); |
| 83 | usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 84 | |
| 85 | retval = strict_strtoul(buf, 10, &profile); |
| 86 | if (retval) |
| 87 | return retval; |
| 88 | |
| 89 | if (profile > 4) |
| 90 | return -EINVAL; |
| 91 | |
| 92 | mutex_lock(&isku->isku_lock); |
| 93 | |
| 94 | retval = isku_set_actual_profile(usb_dev, profile); |
| 95 | if (retval) { |
| 96 | mutex_unlock(&isku->isku_lock); |
| 97 | return retval; |
| 98 | } |
| 99 | |
| 100 | isku_profile_activated(isku, profile); |
| 101 | |
| 102 | roccat_report.event = ISKU_REPORT_BUTTON_EVENT_PROFILE; |
| 103 | roccat_report.data1 = profile + 1; |
| 104 | roccat_report.data2 = 0; |
| 105 | roccat_report.profile = profile + 1; |
| 106 | roccat_report_event(isku->chrdev_minor, (uint8_t const *)&roccat_report); |
| 107 | |
| 108 | mutex_unlock(&isku->isku_lock); |
| 109 | |
| 110 | return size; |
| 111 | } |
| 112 | |
| 113 | static struct device_attribute isku_attributes[] = { |
| 114 | __ATTR(actual_profile, 0660, |
| 115 | isku_sysfs_show_actual_profile, |
| 116 | isku_sysfs_set_actual_profile), |
| 117 | __ATTR_NULL |
| 118 | }; |
| 119 | |
| 120 | static ssize_t isku_sysfs_read(struct file *fp, struct kobject *kobj, |
| 121 | char *buf, loff_t off, size_t count, |
| 122 | size_t real_size, uint command) |
| 123 | { |
| 124 | struct device *dev = |
| 125 | container_of(kobj, struct device, kobj)->parent->parent; |
| 126 | struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev)); |
| 127 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 128 | int retval; |
| 129 | |
| 130 | if (off >= real_size) |
| 131 | return 0; |
| 132 | |
| 133 | if (off != 0 || count != real_size) |
| 134 | return -EINVAL; |
| 135 | |
| 136 | mutex_lock(&isku->isku_lock); |
| 137 | retval = isku_receive(usb_dev, command, buf, real_size); |
| 138 | mutex_unlock(&isku->isku_lock); |
| 139 | |
| 140 | return retval ? retval : real_size; |
| 141 | } |
| 142 | |
| 143 | static ssize_t isku_sysfs_write(struct file *fp, struct kobject *kobj, |
| 144 | void const *buf, loff_t off, size_t count, |
| 145 | size_t real_size, uint command) |
| 146 | { |
| 147 | struct device *dev = |
| 148 | container_of(kobj, struct device, kobj)->parent->parent; |
| 149 | struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev)); |
| 150 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 151 | int retval; |
| 152 | |
| 153 | if (off != 0 || count != real_size) |
| 154 | return -EINVAL; |
| 155 | |
| 156 | mutex_lock(&isku->isku_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 157 | retval = roccat_common2_send_with_status(usb_dev, command, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 158 | (void *)buf, real_size); |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 159 | mutex_unlock(&isku->isku_lock); |
| 160 | |
| 161 | return retval ? retval : real_size; |
| 162 | } |
| 163 | |
| 164 | #define ISKU_SYSFS_W(thingy, THINGY) \ |
| 165 | static ssize_t isku_sysfs_write_ ## thingy(struct file *fp, struct kobject *kobj, \ |
| 166 | struct bin_attribute *attr, char *buf, \ |
| 167 | loff_t off, size_t count) \ |
| 168 | { \ |
| 169 | return isku_sysfs_write(fp, kobj, buf, off, count, \ |
Stefan Achatz | 6e5920d | 2012-11-11 06:21:02 +0100 | [diff] [blame] | 170 | ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \ |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | #define ISKU_SYSFS_R(thingy, THINGY) \ |
| 174 | static ssize_t isku_sysfs_read_ ## thingy(struct file *fp, struct kobject *kobj, \ |
| 175 | struct bin_attribute *attr, char *buf, \ |
| 176 | loff_t off, size_t count) \ |
| 177 | { \ |
| 178 | return isku_sysfs_read(fp, kobj, buf, off, count, \ |
Stefan Achatz | 6e5920d | 2012-11-11 06:21:02 +0100 | [diff] [blame] | 179 | ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \ |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | #define ISKU_SYSFS_RW(thingy, THINGY) \ |
| 183 | ISKU_SYSFS_R(thingy, THINGY) \ |
| 184 | ISKU_SYSFS_W(thingy, THINGY) |
| 185 | |
Stefan Achatz | 6e5920d | 2012-11-11 06:21:02 +0100 | [diff] [blame] | 186 | #define ISKU_BIN_ATTR_RW(thingy, THINGY) \ |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 187 | { \ |
| 188 | .attr = { .name = #thingy, .mode = 0660 }, \ |
Stefan Achatz | 6e5920d | 2012-11-11 06:21:02 +0100 | [diff] [blame] | 189 | .size = ISKU_SIZE_ ## THINGY, \ |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 190 | .read = isku_sysfs_read_ ## thingy, \ |
| 191 | .write = isku_sysfs_write_ ## thingy \ |
| 192 | } |
| 193 | |
Stefan Achatz | 6e5920d | 2012-11-11 06:21:02 +0100 | [diff] [blame] | 194 | #define ISKU_BIN_ATTR_R(thingy, THINGY) \ |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 195 | { \ |
| 196 | .attr = { .name = #thingy, .mode = 0440 }, \ |
Stefan Achatz | 6e5920d | 2012-11-11 06:21:02 +0100 | [diff] [blame] | 197 | .size = ISKU_SIZE_ ## THINGY, \ |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 198 | .read = isku_sysfs_read_ ## thingy, \ |
| 199 | } |
| 200 | |
Stefan Achatz | 6e5920d | 2012-11-11 06:21:02 +0100 | [diff] [blame] | 201 | #define ISKU_BIN_ATTR_W(thingy, THINGY) \ |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 202 | { \ |
| 203 | .attr = { .name = #thingy, .mode = 0220 }, \ |
Stefan Achatz | 6e5920d | 2012-11-11 06:21:02 +0100 | [diff] [blame] | 204 | .size = ISKU_SIZE_ ## THINGY, \ |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 205 | .write = isku_sysfs_write_ ## thingy \ |
| 206 | } |
| 207 | |
| 208 | ISKU_SYSFS_RW(macro, MACRO) |
| 209 | ISKU_SYSFS_RW(keys_function, KEYS_FUNCTION) |
| 210 | ISKU_SYSFS_RW(keys_easyzone, KEYS_EASYZONE) |
| 211 | ISKU_SYSFS_RW(keys_media, KEYS_MEDIA) |
| 212 | ISKU_SYSFS_RW(keys_thumbster, KEYS_THUMBSTER) |
| 213 | ISKU_SYSFS_RW(keys_macro, KEYS_MACRO) |
| 214 | ISKU_SYSFS_RW(keys_capslock, KEYS_CAPSLOCK) |
| 215 | ISKU_SYSFS_RW(light, LIGHT) |
| 216 | ISKU_SYSFS_RW(key_mask, KEY_MASK) |
| 217 | ISKU_SYSFS_RW(last_set, LAST_SET) |
| 218 | ISKU_SYSFS_W(talk, TALK) |
| 219 | ISKU_SYSFS_R(info, INFO) |
| 220 | ISKU_SYSFS_W(control, CONTROL) |
Stefan Achatz | f1da71e | 2012-11-11 06:21:06 +0100 | [diff] [blame] | 221 | ISKU_SYSFS_W(reset, RESET) |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 222 | |
| 223 | static struct bin_attribute isku_bin_attributes[] = { |
Stefan Achatz | 6e5920d | 2012-11-11 06:21:02 +0100 | [diff] [blame] | 224 | ISKU_BIN_ATTR_RW(macro, MACRO), |
| 225 | ISKU_BIN_ATTR_RW(keys_function, KEYS_FUNCTION), |
| 226 | ISKU_BIN_ATTR_RW(keys_easyzone, KEYS_EASYZONE), |
| 227 | ISKU_BIN_ATTR_RW(keys_media, KEYS_MEDIA), |
| 228 | ISKU_BIN_ATTR_RW(keys_thumbster, KEYS_THUMBSTER), |
| 229 | ISKU_BIN_ATTR_RW(keys_macro, KEYS_MACRO), |
| 230 | ISKU_BIN_ATTR_RW(keys_capslock, KEYS_CAPSLOCK), |
| 231 | ISKU_BIN_ATTR_RW(light, LIGHT), |
| 232 | ISKU_BIN_ATTR_RW(key_mask, KEY_MASK), |
| 233 | ISKU_BIN_ATTR_RW(last_set, LAST_SET), |
| 234 | ISKU_BIN_ATTR_W(talk, TALK), |
| 235 | ISKU_BIN_ATTR_R(info, INFO), |
| 236 | ISKU_BIN_ATTR_W(control, CONTROL), |
Stefan Achatz | f1da71e | 2012-11-11 06:21:06 +0100 | [diff] [blame] | 237 | ISKU_BIN_ATTR_W(reset, RESET), |
Stefan Achatz | d41c2a7 | 2011-11-24 17:46:24 +0100 | [diff] [blame] | 238 | __ATTR_NULL |
| 239 | }; |
| 240 | |
| 241 | static int isku_init_isku_device_struct(struct usb_device *usb_dev, |
| 242 | struct isku_device *isku) |
| 243 | { |
| 244 | int retval; |
| 245 | |
| 246 | mutex_init(&isku->isku_lock); |
| 247 | |
| 248 | retval = isku_get_actual_profile(usb_dev); |
| 249 | if (retval < 0) |
| 250 | return retval; |
| 251 | isku_profile_activated(isku, retval); |
| 252 | |
| 253 | return 0; |
| 254 | } |
| 255 | |
| 256 | static int isku_init_specials(struct hid_device *hdev) |
| 257 | { |
| 258 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 259 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 260 | struct isku_device *isku; |
| 261 | int retval; |
| 262 | |
| 263 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 264 | != ISKU_USB_INTERFACE_PROTOCOL) { |
| 265 | hid_set_drvdata(hdev, NULL); |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | isku = kzalloc(sizeof(*isku), GFP_KERNEL); |
| 270 | if (!isku) { |
| 271 | hid_err(hdev, "can't alloc device descriptor\n"); |
| 272 | return -ENOMEM; |
| 273 | } |
| 274 | hid_set_drvdata(hdev, isku); |
| 275 | |
| 276 | retval = isku_init_isku_device_struct(usb_dev, isku); |
| 277 | if (retval) { |
| 278 | hid_err(hdev, "couldn't init struct isku_device\n"); |
| 279 | goto exit_free; |
| 280 | } |
| 281 | |
| 282 | retval = roccat_connect(isku_class, hdev, |
| 283 | sizeof(struct isku_roccat_report)); |
| 284 | if (retval < 0) { |
| 285 | hid_err(hdev, "couldn't init char dev\n"); |
| 286 | } else { |
| 287 | isku->chrdev_minor = retval; |
| 288 | isku->roccat_claimed = 1; |
| 289 | } |
| 290 | |
| 291 | return 0; |
| 292 | exit_free: |
| 293 | kfree(isku); |
| 294 | return retval; |
| 295 | } |
| 296 | |
| 297 | static void isku_remove_specials(struct hid_device *hdev) |
| 298 | { |
| 299 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 300 | struct isku_device *isku; |
| 301 | |
| 302 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 303 | != ISKU_USB_INTERFACE_PROTOCOL) |
| 304 | return; |
| 305 | |
| 306 | isku = hid_get_drvdata(hdev); |
| 307 | if (isku->roccat_claimed) |
| 308 | roccat_disconnect(isku->chrdev_minor); |
| 309 | kfree(isku); |
| 310 | } |
| 311 | |
| 312 | static int isku_probe(struct hid_device *hdev, |
| 313 | const struct hid_device_id *id) |
| 314 | { |
| 315 | int retval; |
| 316 | |
| 317 | retval = hid_parse(hdev); |
| 318 | if (retval) { |
| 319 | hid_err(hdev, "parse failed\n"); |
| 320 | goto exit; |
| 321 | } |
| 322 | |
| 323 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 324 | if (retval) { |
| 325 | hid_err(hdev, "hw start failed\n"); |
| 326 | goto exit; |
| 327 | } |
| 328 | |
| 329 | retval = isku_init_specials(hdev); |
| 330 | if (retval) { |
| 331 | hid_err(hdev, "couldn't install keyboard\n"); |
| 332 | goto exit_stop; |
| 333 | } |
| 334 | |
| 335 | return 0; |
| 336 | |
| 337 | exit_stop: |
| 338 | hid_hw_stop(hdev); |
| 339 | exit: |
| 340 | return retval; |
| 341 | } |
| 342 | |
| 343 | static void isku_remove(struct hid_device *hdev) |
| 344 | { |
| 345 | isku_remove_specials(hdev); |
| 346 | hid_hw_stop(hdev); |
| 347 | } |
| 348 | |
| 349 | static void isku_keep_values_up_to_date(struct isku_device *isku, |
| 350 | u8 const *data) |
| 351 | { |
| 352 | struct isku_report_button const *button_report; |
| 353 | |
| 354 | switch (data[0]) { |
| 355 | case ISKU_REPORT_NUMBER_BUTTON: |
| 356 | button_report = (struct isku_report_button const *)data; |
| 357 | switch (button_report->event) { |
| 358 | case ISKU_REPORT_BUTTON_EVENT_PROFILE: |
| 359 | isku_profile_activated(isku, button_report->data1 - 1); |
| 360 | break; |
| 361 | } |
| 362 | break; |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | static void isku_report_to_chrdev(struct isku_device const *isku, |
| 367 | u8 const *data) |
| 368 | { |
| 369 | struct isku_roccat_report roccat_report; |
| 370 | struct isku_report_button const *button_report; |
| 371 | |
| 372 | if (data[0] != ISKU_REPORT_NUMBER_BUTTON) |
| 373 | return; |
| 374 | |
| 375 | button_report = (struct isku_report_button const *)data; |
| 376 | |
| 377 | roccat_report.event = button_report->event; |
| 378 | roccat_report.data1 = button_report->data1; |
| 379 | roccat_report.data2 = button_report->data2; |
| 380 | roccat_report.profile = isku->actual_profile + 1; |
| 381 | roccat_report_event(isku->chrdev_minor, |
| 382 | (uint8_t const *)&roccat_report); |
| 383 | } |
| 384 | |
| 385 | static int isku_raw_event(struct hid_device *hdev, |
| 386 | struct hid_report *report, u8 *data, int size) |
| 387 | { |
| 388 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 389 | struct isku_device *isku = hid_get_drvdata(hdev); |
| 390 | |
| 391 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 392 | != ISKU_USB_INTERFACE_PROTOCOL) |
| 393 | return 0; |
| 394 | |
| 395 | if (isku == NULL) |
| 396 | return 0; |
| 397 | |
| 398 | isku_keep_values_up_to_date(isku, data); |
| 399 | |
| 400 | if (isku->roccat_claimed) |
| 401 | isku_report_to_chrdev(isku, data); |
| 402 | |
| 403 | return 0; |
| 404 | } |
| 405 | |
| 406 | static const struct hid_device_id isku_devices[] = { |
| 407 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) }, |
| 408 | { } |
| 409 | }; |
| 410 | |
| 411 | MODULE_DEVICE_TABLE(hid, isku_devices); |
| 412 | |
| 413 | static struct hid_driver isku_driver = { |
| 414 | .name = "isku", |
| 415 | .id_table = isku_devices, |
| 416 | .probe = isku_probe, |
| 417 | .remove = isku_remove, |
| 418 | .raw_event = isku_raw_event |
| 419 | }; |
| 420 | |
| 421 | static int __init isku_init(void) |
| 422 | { |
| 423 | int retval; |
| 424 | isku_class = class_create(THIS_MODULE, "isku"); |
| 425 | if (IS_ERR(isku_class)) |
| 426 | return PTR_ERR(isku_class); |
| 427 | isku_class->dev_attrs = isku_attributes; |
| 428 | isku_class->dev_bin_attrs = isku_bin_attributes; |
| 429 | |
| 430 | retval = hid_register_driver(&isku_driver); |
| 431 | if (retval) |
| 432 | class_destroy(isku_class); |
| 433 | return retval; |
| 434 | } |
| 435 | |
| 436 | static void __exit isku_exit(void) |
| 437 | { |
| 438 | hid_unregister_driver(&isku_driver); |
| 439 | class_destroy(isku_class); |
| 440 | } |
| 441 | |
| 442 | module_init(isku_init); |
| 443 | module_exit(isku_exit); |
| 444 | |
| 445 | MODULE_AUTHOR("Stefan Achatz"); |
| 446 | MODULE_DESCRIPTION("USB Roccat Isku driver"); |
| 447 | MODULE_LICENSE("GPL v2"); |