Stefan Achatz | 6a2a639 | 2012-05-20 22:44:54 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Roccat Savu driver for Linux |
| 3 | * |
| 4 | * Copyright (c) 2012 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 | /* Roccat Savu is a gamer mouse with macro keys that can be configured in |
| 15 | * 5 profiles. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/device.h> |
| 19 | #include <linux/input.h> |
| 20 | #include <linux/hid.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/hid-roccat.h> |
| 24 | #include "hid-ids.h" |
| 25 | #include "hid-roccat-common.h" |
| 26 | #include "hid-roccat-savu.h" |
| 27 | |
| 28 | static struct class *savu_class; |
| 29 | |
Stefan Achatz | 6a2a639 | 2012-05-20 22:44:54 +0200 | [diff] [blame] | 30 | static ssize_t savu_sysfs_read(struct file *fp, struct kobject *kobj, |
| 31 | char *buf, loff_t off, size_t count, |
| 32 | size_t real_size, uint command) |
| 33 | { |
| 34 | struct device *dev = |
| 35 | container_of(kobj, struct device, kobj)->parent->parent; |
| 36 | struct savu_device *savu = hid_get_drvdata(dev_get_drvdata(dev)); |
| 37 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 38 | int retval; |
| 39 | |
| 40 | if (off >= real_size) |
| 41 | return 0; |
| 42 | |
| 43 | if (off != 0 || count != real_size) |
| 44 | return -EINVAL; |
| 45 | |
| 46 | mutex_lock(&savu->savu_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 47 | retval = roccat_common2_receive(usb_dev, command, buf, real_size); |
Stefan Achatz | 6a2a639 | 2012-05-20 22:44:54 +0200 | [diff] [blame] | 48 | mutex_unlock(&savu->savu_lock); |
| 49 | |
| 50 | return retval ? retval : real_size; |
| 51 | } |
| 52 | |
| 53 | static ssize_t savu_sysfs_write(struct file *fp, struct kobject *kobj, |
| 54 | void const *buf, loff_t off, size_t count, |
| 55 | size_t real_size, uint command) |
| 56 | { |
| 57 | struct device *dev = |
| 58 | container_of(kobj, struct device, kobj)->parent->parent; |
| 59 | struct savu_device *savu = hid_get_drvdata(dev_get_drvdata(dev)); |
| 60 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 61 | int retval; |
| 62 | |
| 63 | if (off != 0 || count != real_size) |
| 64 | return -EINVAL; |
| 65 | |
| 66 | mutex_lock(&savu->savu_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 67 | retval = roccat_common2_send_with_status(usb_dev, command, |
Stefan Achatz | 4728f2d | 2012-05-20 22:44:59 +0200 | [diff] [blame] | 68 | (void *)buf, real_size); |
Stefan Achatz | 6a2a639 | 2012-05-20 22:44:54 +0200 | [diff] [blame] | 69 | mutex_unlock(&savu->savu_lock); |
| 70 | |
| 71 | return retval ? retval : real_size; |
| 72 | } |
| 73 | |
| 74 | #define SAVU_SYSFS_W(thingy, THINGY) \ |
| 75 | static ssize_t savu_sysfs_write_ ## thingy(struct file *fp, \ |
| 76 | struct kobject *kobj, struct bin_attribute *attr, char *buf, \ |
| 77 | loff_t off, size_t count) \ |
| 78 | { \ |
| 79 | return savu_sysfs_write(fp, kobj, buf, off, count, \ |
| 80 | SAVU_SIZE_ ## THINGY, SAVU_COMMAND_ ## THINGY); \ |
| 81 | } |
| 82 | |
| 83 | #define SAVU_SYSFS_R(thingy, THINGY) \ |
| 84 | static ssize_t savu_sysfs_read_ ## thingy(struct file *fp, \ |
| 85 | struct kobject *kobj, struct bin_attribute *attr, char *buf, \ |
| 86 | loff_t off, size_t count) \ |
| 87 | { \ |
| 88 | return savu_sysfs_read(fp, kobj, buf, off, count, \ |
| 89 | SAVU_SIZE_ ## THINGY, SAVU_COMMAND_ ## THINGY); \ |
| 90 | } |
| 91 | |
| 92 | #define SAVU_SYSFS_RW(thingy, THINGY) \ |
| 93 | SAVU_SYSFS_W(thingy, THINGY) \ |
| 94 | SAVU_SYSFS_R(thingy, THINGY) |
| 95 | |
| 96 | #define SAVU_BIN_ATTRIBUTE_RW(thingy, THINGY) \ |
| 97 | { \ |
| 98 | .attr = { .name = #thingy, .mode = 0660 }, \ |
| 99 | .size = SAVU_SIZE_ ## THINGY, \ |
| 100 | .read = savu_sysfs_read_ ## thingy, \ |
| 101 | .write = savu_sysfs_write_ ## thingy \ |
| 102 | } |
| 103 | |
| 104 | #define SAVU_BIN_ATTRIBUTE_R(thingy, THINGY) \ |
| 105 | { \ |
| 106 | .attr = { .name = #thingy, .mode = 0440 }, \ |
| 107 | .size = SAVU_SIZE_ ## THINGY, \ |
| 108 | .read = savu_sysfs_read_ ## thingy, \ |
| 109 | } |
| 110 | |
| 111 | #define SAVU_BIN_ATTRIBUTE_W(thingy, THINGY) \ |
| 112 | { \ |
| 113 | .attr = { .name = #thingy, .mode = 0220 }, \ |
| 114 | .size = SAVU_SIZE_ ## THINGY, \ |
| 115 | .write = savu_sysfs_write_ ## thingy \ |
| 116 | } |
| 117 | |
| 118 | SAVU_SYSFS_W(control, CONTROL) |
| 119 | SAVU_SYSFS_RW(profile, PROFILE) |
| 120 | SAVU_SYSFS_RW(general, GENERAL) |
| 121 | SAVU_SYSFS_RW(buttons, BUTTONS) |
| 122 | SAVU_SYSFS_RW(macro, MACRO) |
| 123 | SAVU_SYSFS_R(info, INFO) |
Stefan Achatz | 11a5c81 | 2012-07-04 18:45:03 +0200 | [diff] [blame] | 124 | SAVU_SYSFS_RW(sensor, SENSOR) |
Stefan Achatz | 6a2a639 | 2012-05-20 22:44:54 +0200 | [diff] [blame] | 125 | |
| 126 | static struct bin_attribute savu_bin_attributes[] = { |
| 127 | SAVU_BIN_ATTRIBUTE_W(control, CONTROL), |
| 128 | SAVU_BIN_ATTRIBUTE_RW(profile, PROFILE), |
| 129 | SAVU_BIN_ATTRIBUTE_RW(general, GENERAL), |
| 130 | SAVU_BIN_ATTRIBUTE_RW(buttons, BUTTONS), |
| 131 | SAVU_BIN_ATTRIBUTE_RW(macro, MACRO), |
| 132 | SAVU_BIN_ATTRIBUTE_R(info, INFO), |
Stefan Achatz | 11a5c81 | 2012-07-04 18:45:03 +0200 | [diff] [blame] | 133 | SAVU_BIN_ATTRIBUTE_RW(sensor, SENSOR), |
Stefan Achatz | 6a2a639 | 2012-05-20 22:44:54 +0200 | [diff] [blame] | 134 | __ATTR_NULL |
| 135 | }; |
| 136 | |
| 137 | static int savu_init_savu_device_struct(struct usb_device *usb_dev, |
| 138 | struct savu_device *savu) |
| 139 | { |
| 140 | mutex_init(&savu->savu_lock); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int savu_init_specials(struct hid_device *hdev) |
| 146 | { |
| 147 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 148 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 149 | struct savu_device *savu; |
| 150 | int retval; |
| 151 | |
| 152 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 153 | != USB_INTERFACE_PROTOCOL_MOUSE) { |
| 154 | hid_set_drvdata(hdev, NULL); |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | savu = kzalloc(sizeof(*savu), GFP_KERNEL); |
| 159 | if (!savu) { |
| 160 | hid_err(hdev, "can't alloc device descriptor\n"); |
| 161 | return -ENOMEM; |
| 162 | } |
| 163 | hid_set_drvdata(hdev, savu); |
| 164 | |
| 165 | retval = savu_init_savu_device_struct(usb_dev, savu); |
| 166 | if (retval) { |
| 167 | hid_err(hdev, "couldn't init struct savu_device\n"); |
| 168 | goto exit_free; |
| 169 | } |
| 170 | |
| 171 | retval = roccat_connect(savu_class, hdev, |
| 172 | sizeof(struct savu_roccat_report)); |
| 173 | if (retval < 0) { |
| 174 | hid_err(hdev, "couldn't init char dev\n"); |
| 175 | } else { |
| 176 | savu->chrdev_minor = retval; |
| 177 | savu->roccat_claimed = 1; |
| 178 | } |
| 179 | |
| 180 | return 0; |
| 181 | exit_free: |
| 182 | kfree(savu); |
| 183 | return retval; |
| 184 | } |
| 185 | |
| 186 | static void savu_remove_specials(struct hid_device *hdev) |
| 187 | { |
| 188 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 189 | struct savu_device *savu; |
| 190 | |
| 191 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 192 | != USB_INTERFACE_PROTOCOL_MOUSE) |
| 193 | return; |
| 194 | |
| 195 | savu = hid_get_drvdata(hdev); |
| 196 | if (savu->roccat_claimed) |
| 197 | roccat_disconnect(savu->chrdev_minor); |
| 198 | kfree(savu); |
| 199 | } |
| 200 | |
| 201 | static int savu_probe(struct hid_device *hdev, |
| 202 | const struct hid_device_id *id) |
| 203 | { |
| 204 | int retval; |
| 205 | |
| 206 | retval = hid_parse(hdev); |
| 207 | if (retval) { |
| 208 | hid_err(hdev, "parse failed\n"); |
| 209 | goto exit; |
| 210 | } |
| 211 | |
| 212 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 213 | if (retval) { |
| 214 | hid_err(hdev, "hw start failed\n"); |
| 215 | goto exit; |
| 216 | } |
| 217 | |
| 218 | retval = savu_init_specials(hdev); |
| 219 | if (retval) { |
| 220 | hid_err(hdev, "couldn't install mouse\n"); |
| 221 | goto exit_stop; |
| 222 | } |
| 223 | |
| 224 | return 0; |
| 225 | |
| 226 | exit_stop: |
| 227 | hid_hw_stop(hdev); |
| 228 | exit: |
| 229 | return retval; |
| 230 | } |
| 231 | |
| 232 | static void savu_remove(struct hid_device *hdev) |
| 233 | { |
| 234 | savu_remove_specials(hdev); |
| 235 | hid_hw_stop(hdev); |
| 236 | } |
| 237 | |
| 238 | static void savu_report_to_chrdev(struct savu_device const *savu, |
| 239 | u8 const *data) |
| 240 | { |
| 241 | struct savu_roccat_report roccat_report; |
| 242 | struct savu_mouse_report_special const *special_report; |
| 243 | |
| 244 | if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL) |
| 245 | return; |
| 246 | |
| 247 | special_report = (struct savu_mouse_report_special const *)data; |
| 248 | |
| 249 | roccat_report.type = special_report->type; |
| 250 | roccat_report.data[0] = special_report->data[0]; |
| 251 | roccat_report.data[1] = special_report->data[1]; |
| 252 | roccat_report_event(savu->chrdev_minor, |
| 253 | (uint8_t const *)&roccat_report); |
| 254 | } |
| 255 | |
| 256 | static int savu_raw_event(struct hid_device *hdev, |
| 257 | struct hid_report *report, u8 *data, int size) |
| 258 | { |
| 259 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 260 | struct savu_device *savu = hid_get_drvdata(hdev); |
| 261 | |
| 262 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 263 | != USB_INTERFACE_PROTOCOL_MOUSE) |
| 264 | return 0; |
| 265 | |
| 266 | if (savu == NULL) |
| 267 | return 0; |
| 268 | |
| 269 | if (savu->roccat_claimed) |
| 270 | savu_report_to_chrdev(savu, data); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
| 275 | static const struct hid_device_id savu_devices[] = { |
| 276 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) }, |
| 277 | { } |
| 278 | }; |
| 279 | |
| 280 | MODULE_DEVICE_TABLE(hid, savu_devices); |
| 281 | |
| 282 | static struct hid_driver savu_driver = { |
| 283 | .name = "savu", |
| 284 | .id_table = savu_devices, |
| 285 | .probe = savu_probe, |
| 286 | .remove = savu_remove, |
| 287 | .raw_event = savu_raw_event |
| 288 | }; |
| 289 | |
| 290 | static int __init savu_init(void) |
| 291 | { |
| 292 | int retval; |
| 293 | |
| 294 | savu_class = class_create(THIS_MODULE, "savu"); |
| 295 | if (IS_ERR(savu_class)) |
| 296 | return PTR_ERR(savu_class); |
| 297 | savu_class->dev_bin_attrs = savu_bin_attributes; |
| 298 | |
| 299 | retval = hid_register_driver(&savu_driver); |
| 300 | if (retval) |
| 301 | class_destroy(savu_class); |
| 302 | return retval; |
| 303 | } |
| 304 | |
| 305 | static void __exit savu_exit(void) |
| 306 | { |
| 307 | hid_unregister_driver(&savu_driver); |
| 308 | class_destroy(savu_class); |
| 309 | } |
| 310 | |
| 311 | module_init(savu_init); |
| 312 | module_exit(savu_exit); |
| 313 | |
| 314 | MODULE_AUTHOR("Stefan Achatz"); |
| 315 | MODULE_DESCRIPTION("USB Roccat Savu driver"); |
| 316 | MODULE_LICENSE("GPL v2"); |