Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Roccat Arvo 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 Arvo is a gamer keyboard with 5 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> |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 22 | #include <linux/module.h> |
| 23 | #include <linux/slab.h> |
Stefan Achatz | 5dc0c98 | 2011-02-03 16:14:43 +0100 | [diff] [blame] | 24 | #include <linux/hid-roccat.h> |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 25 | #include "hid-ids.h" |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 26 | #include "hid-roccat-common.h" |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 27 | #include "hid-roccat-arvo.h" |
| 28 | |
| 29 | static struct class *arvo_class; |
| 30 | |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 31 | static ssize_t arvo_sysfs_show_mode_key(struct device *dev, |
| 32 | struct device_attribute *attr, char *buf) |
| 33 | { |
| 34 | struct arvo_device *arvo = |
| 35 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 36 | struct usb_device *usb_dev = |
| 37 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 38 | struct arvo_mode_key temp_buf; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 39 | int retval; |
| 40 | |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 41 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 42 | retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_MODE_KEY, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 43 | &temp_buf, sizeof(struct arvo_mode_key)); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 44 | mutex_unlock(&arvo->arvo_lock); |
| 45 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 46 | return retval; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 47 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 48 | return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.state); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static ssize_t arvo_sysfs_set_mode_key(struct device *dev, |
| 52 | struct device_attribute *attr, char const *buf, size_t size) |
| 53 | { |
| 54 | struct arvo_device *arvo = |
| 55 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 56 | struct usb_device *usb_dev = |
| 57 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 58 | struct arvo_mode_key temp_buf; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 59 | unsigned long state; |
| 60 | int retval; |
| 61 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 62 | retval = kstrtoul(buf, 10, &state); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 63 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 64 | return retval; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 65 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 66 | temp_buf.command = ARVO_COMMAND_MODE_KEY; |
| 67 | temp_buf.state = state; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 68 | |
| 69 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 70 | retval = roccat_common2_send(usb_dev, ARVO_COMMAND_MODE_KEY, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 71 | &temp_buf, sizeof(struct arvo_mode_key)); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 72 | mutex_unlock(&arvo->arvo_lock); |
| 73 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 74 | return retval; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 75 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 76 | return size; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 77 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 78 | static DEVICE_ATTR(mode_key, 0660, |
| 79 | arvo_sysfs_show_mode_key, arvo_sysfs_set_mode_key); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 80 | |
| 81 | static ssize_t arvo_sysfs_show_key_mask(struct device *dev, |
| 82 | struct device_attribute *attr, char *buf) |
| 83 | { |
| 84 | struct arvo_device *arvo = |
| 85 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 86 | struct usb_device *usb_dev = |
| 87 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 88 | struct arvo_key_mask temp_buf; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 89 | int retval; |
| 90 | |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 91 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 92 | retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_KEY_MASK, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 93 | &temp_buf, sizeof(struct arvo_key_mask)); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 94 | mutex_unlock(&arvo->arvo_lock); |
| 95 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 96 | return retval; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 97 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 98 | return snprintf(buf, PAGE_SIZE, "%d\n", temp_buf.key_mask); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | static ssize_t arvo_sysfs_set_key_mask(struct device *dev, |
| 102 | struct device_attribute *attr, char const *buf, size_t size) |
| 103 | { |
| 104 | struct arvo_device *arvo = |
| 105 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 106 | struct usb_device *usb_dev = |
| 107 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 108 | struct arvo_key_mask temp_buf; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 109 | unsigned long key_mask; |
| 110 | int retval; |
| 111 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 112 | retval = kstrtoul(buf, 10, &key_mask); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 113 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 114 | return retval; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 115 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 116 | temp_buf.command = ARVO_COMMAND_KEY_MASK; |
| 117 | temp_buf.key_mask = key_mask; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 118 | |
| 119 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 120 | retval = roccat_common2_send(usb_dev, ARVO_COMMAND_KEY_MASK, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 121 | &temp_buf, sizeof(struct arvo_key_mask)); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 122 | mutex_unlock(&arvo->arvo_lock); |
| 123 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 124 | return retval; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 125 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 126 | return size; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 127 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 128 | static DEVICE_ATTR(key_mask, 0660, |
| 129 | arvo_sysfs_show_key_mask, arvo_sysfs_set_key_mask); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 130 | |
| 131 | /* retval is 1-5 on success, < 0 on error */ |
| 132 | static int arvo_get_actual_profile(struct usb_device *usb_dev) |
| 133 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 134 | struct arvo_actual_profile temp_buf; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 135 | int retval; |
| 136 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 137 | retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 138 | &temp_buf, sizeof(struct arvo_actual_profile)); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 139 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 140 | if (retval) |
| 141 | return retval; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 142 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 143 | return temp_buf.actual_profile; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static ssize_t arvo_sysfs_show_actual_profile(struct device *dev, |
| 147 | struct device_attribute *attr, char *buf) |
| 148 | { |
| 149 | struct arvo_device *arvo = |
| 150 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 151 | |
| 152 | return snprintf(buf, PAGE_SIZE, "%d\n", arvo->actual_profile); |
| 153 | } |
| 154 | |
| 155 | static ssize_t arvo_sysfs_set_actual_profile(struct device *dev, |
| 156 | struct device_attribute *attr, char const *buf, size_t size) |
| 157 | { |
| 158 | struct arvo_device *arvo = |
| 159 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 160 | struct usb_device *usb_dev = |
| 161 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 162 | struct arvo_actual_profile temp_buf; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 163 | unsigned long profile; |
| 164 | int retval; |
| 165 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 166 | retval = kstrtoul(buf, 10, &profile); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 167 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 168 | return retval; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 169 | |
Stefan Achatz | 901e64d | 2011-06-12 10:02:44 +0200 | [diff] [blame] | 170 | if (profile < 1 || profile > 5) |
| 171 | return -EINVAL; |
| 172 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 173 | temp_buf.command = ARVO_COMMAND_ACTUAL_PROFILE; |
| 174 | temp_buf.actual_profile = profile; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 175 | |
| 176 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 177 | retval = roccat_common2_send(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 178 | &temp_buf, sizeof(struct arvo_actual_profile)); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 179 | if (!retval) { |
| 180 | arvo->actual_profile = profile; |
| 181 | retval = size; |
| 182 | } |
| 183 | mutex_unlock(&arvo->arvo_lock); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 184 | return retval; |
| 185 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 186 | static DEVICE_ATTR(actual_profile, 0660, |
| 187 | arvo_sysfs_show_actual_profile, |
| 188 | arvo_sysfs_set_actual_profile); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 189 | |
| 190 | static ssize_t arvo_sysfs_write(struct file *fp, |
| 191 | struct kobject *kobj, void const *buf, |
| 192 | loff_t off, size_t count, size_t real_size, uint command) |
| 193 | { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 194 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 195 | struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev)); |
| 196 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 197 | int retval; |
| 198 | |
| 199 | if (off != 0 || count != real_size) |
| 200 | return -EINVAL; |
| 201 | |
| 202 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 203 | retval = roccat_common2_send(usb_dev, command, buf, real_size); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 204 | mutex_unlock(&arvo->arvo_lock); |
| 205 | |
| 206 | return (retval ? retval : real_size); |
| 207 | } |
| 208 | |
| 209 | static ssize_t arvo_sysfs_read(struct file *fp, |
| 210 | struct kobject *kobj, void *buf, loff_t off, |
| 211 | size_t count, size_t real_size, uint command) |
| 212 | { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 213 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 214 | struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev)); |
| 215 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 216 | int retval; |
| 217 | |
| 218 | if (off >= real_size) |
| 219 | return 0; |
| 220 | |
| 221 | if (off != 0 || count != real_size) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 225 | retval = roccat_common2_receive(usb_dev, command, buf, real_size); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 226 | mutex_unlock(&arvo->arvo_lock); |
| 227 | |
| 228 | return (retval ? retval : real_size); |
| 229 | } |
| 230 | |
| 231 | static ssize_t arvo_sysfs_write_button(struct file *fp, |
| 232 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 233 | loff_t off, size_t count) |
| 234 | { |
| 235 | return arvo_sysfs_write(fp, kobj, buf, off, count, |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 236 | sizeof(struct arvo_button), ARVO_COMMAND_BUTTON); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 237 | } |
Greg Kroah-Hartman | e0a00d8 | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 238 | static BIN_ATTR(button, 0220, NULL, arvo_sysfs_write_button, |
| 239 | sizeof(struct arvo_button)); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 240 | |
| 241 | static ssize_t arvo_sysfs_read_info(struct file *fp, |
| 242 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 243 | loff_t off, size_t count) |
| 244 | { |
| 245 | return arvo_sysfs_read(fp, kobj, buf, off, count, |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 246 | sizeof(struct arvo_info), ARVO_COMMAND_INFO); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 247 | } |
Greg Kroah-Hartman | e0a00d8 | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 248 | static BIN_ATTR(info, 0440, arvo_sysfs_read_info, NULL, |
| 249 | sizeof(struct arvo_info)); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 250 | |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 251 | static struct attribute *arvo_attrs[] = { |
| 252 | &dev_attr_mode_key.attr, |
| 253 | &dev_attr_key_mask.attr, |
| 254 | &dev_attr_actual_profile.attr, |
| 255 | NULL, |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 256 | }; |
| 257 | |
Greg Kroah-Hartman | e0a00d8 | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 258 | static struct bin_attribute *arvo_bin_attributes[] = { |
| 259 | &bin_attr_button, |
| 260 | &bin_attr_info, |
| 261 | NULL, |
| 262 | }; |
| 263 | |
| 264 | static const struct attribute_group arvo_group = { |
| 265 | .attrs = arvo_attrs, |
| 266 | .bin_attrs = arvo_bin_attributes, |
| 267 | }; |
| 268 | |
| 269 | static const struct attribute_group *arvo_groups[] = { |
| 270 | &arvo_group, |
| 271 | NULL, |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | static int arvo_init_arvo_device_struct(struct usb_device *usb_dev, |
| 275 | struct arvo_device *arvo) |
| 276 | { |
| 277 | int retval; |
| 278 | |
| 279 | mutex_init(&arvo->arvo_lock); |
| 280 | |
| 281 | retval = arvo_get_actual_profile(usb_dev); |
| 282 | if (retval < 0) |
| 283 | return retval; |
| 284 | arvo->actual_profile = retval; |
| 285 | |
| 286 | return 0; |
| 287 | } |
| 288 | |
| 289 | static int arvo_init_specials(struct hid_device *hdev) |
| 290 | { |
| 291 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 292 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 293 | struct arvo_device *arvo; |
| 294 | int retval; |
| 295 | |
| 296 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 297 | == USB_INTERFACE_PROTOCOL_KEYBOARD) { |
| 298 | hid_set_drvdata(hdev, NULL); |
| 299 | return 0; |
| 300 | } |
| 301 | |
| 302 | arvo = kzalloc(sizeof(*arvo), GFP_KERNEL); |
| 303 | if (!arvo) { |
Stefan Achatz | a28764e | 2011-01-30 13:38:21 +0100 | [diff] [blame] | 304 | hid_err(hdev, "can't alloc device descriptor\n"); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 305 | return -ENOMEM; |
| 306 | } |
| 307 | hid_set_drvdata(hdev, arvo); |
| 308 | |
| 309 | retval = arvo_init_arvo_device_struct(usb_dev, arvo); |
| 310 | if (retval) { |
Stefan Achatz | a28764e | 2011-01-30 13:38:21 +0100 | [diff] [blame] | 311 | hid_err(hdev, "couldn't init struct arvo_device\n"); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 312 | goto exit_free; |
| 313 | } |
| 314 | |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 315 | retval = roccat_connect(arvo_class, hdev, |
| 316 | sizeof(struct arvo_roccat_report)); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 317 | if (retval < 0) { |
Stefan Achatz | a28764e | 2011-01-30 13:38:21 +0100 | [diff] [blame] | 318 | hid_err(hdev, "couldn't init char dev\n"); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 319 | } else { |
| 320 | arvo->chrdev_minor = retval; |
| 321 | arvo->roccat_claimed = 1; |
| 322 | } |
| 323 | |
| 324 | return 0; |
| 325 | exit_free: |
| 326 | kfree(arvo); |
| 327 | return retval; |
| 328 | } |
| 329 | |
| 330 | static void arvo_remove_specials(struct hid_device *hdev) |
| 331 | { |
| 332 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 333 | struct arvo_device *arvo; |
| 334 | |
| 335 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 336 | == USB_INTERFACE_PROTOCOL_KEYBOARD) |
| 337 | return; |
| 338 | |
| 339 | arvo = hid_get_drvdata(hdev); |
| 340 | if (arvo->roccat_claimed) |
| 341 | roccat_disconnect(arvo->chrdev_minor); |
| 342 | kfree(arvo); |
| 343 | } |
| 344 | |
| 345 | static int arvo_probe(struct hid_device *hdev, |
| 346 | const struct hid_device_id *id) |
| 347 | { |
| 348 | int retval; |
| 349 | |
| 350 | retval = hid_parse(hdev); |
| 351 | if (retval) { |
Stefan Achatz | a28764e | 2011-01-30 13:38:21 +0100 | [diff] [blame] | 352 | hid_err(hdev, "parse failed\n"); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 353 | goto exit; |
| 354 | } |
| 355 | |
| 356 | retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); |
| 357 | if (retval) { |
Stefan Achatz | a28764e | 2011-01-30 13:38:21 +0100 | [diff] [blame] | 358 | hid_err(hdev, "hw start failed\n"); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 359 | goto exit; |
| 360 | } |
| 361 | |
| 362 | retval = arvo_init_specials(hdev); |
| 363 | if (retval) { |
Stefan Achatz | a28764e | 2011-01-30 13:38:21 +0100 | [diff] [blame] | 364 | hid_err(hdev, "couldn't install keyboard\n"); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 365 | goto exit_stop; |
| 366 | } |
| 367 | |
| 368 | return 0; |
| 369 | |
| 370 | exit_stop: |
| 371 | hid_hw_stop(hdev); |
| 372 | exit: |
| 373 | return retval; |
| 374 | } |
| 375 | |
| 376 | static void arvo_remove(struct hid_device *hdev) |
| 377 | { |
| 378 | arvo_remove_specials(hdev); |
| 379 | hid_hw_stop(hdev); |
| 380 | } |
| 381 | |
| 382 | static void arvo_report_to_chrdev(struct arvo_device const *arvo, |
| 383 | u8 const *data) |
| 384 | { |
| 385 | struct arvo_special_report const *special_report; |
| 386 | struct arvo_roccat_report roccat_report; |
| 387 | |
| 388 | special_report = (struct arvo_special_report const *)data; |
| 389 | |
| 390 | roccat_report.profile = arvo->actual_profile; |
| 391 | roccat_report.button = special_report->event & |
| 392 | ARVO_SPECIAL_REPORT_EVENT_MASK_BUTTON; |
| 393 | if ((special_report->event & ARVO_SPECIAL_REPORT_EVENT_MASK_ACTION) == |
| 394 | ARVO_SPECIAL_REPORT_EVENT_ACTION_PRESS) |
| 395 | roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_PRESS; |
| 396 | else |
| 397 | roccat_report.action = ARVO_ROCCAT_REPORT_ACTION_RELEASE; |
| 398 | |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 399 | roccat_report_event(arvo->chrdev_minor, |
| 400 | (uint8_t const *)&roccat_report); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static int arvo_raw_event(struct hid_device *hdev, |
| 404 | struct hid_report *report, u8 *data, int size) |
| 405 | { |
| 406 | struct arvo_device *arvo = hid_get_drvdata(hdev); |
| 407 | |
| 408 | if (size != 3) |
| 409 | return 0; |
| 410 | |
Stefan Achatz | 901e64d | 2011-06-12 10:02:44 +0200 | [diff] [blame] | 411 | if (arvo && arvo->roccat_claimed) |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 412 | arvo_report_to_chrdev(arvo, data); |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | static const struct hid_device_id arvo_devices[] = { |
| 418 | { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ARVO) }, |
| 419 | { } |
| 420 | }; |
| 421 | |
| 422 | MODULE_DEVICE_TABLE(hid, arvo_devices); |
| 423 | |
| 424 | static struct hid_driver arvo_driver = { |
| 425 | .name = "arvo", |
| 426 | .id_table = arvo_devices, |
| 427 | .probe = arvo_probe, |
| 428 | .remove = arvo_remove, |
| 429 | .raw_event = arvo_raw_event |
| 430 | }; |
| 431 | |
| 432 | static int __init arvo_init(void) |
| 433 | { |
| 434 | int retval; |
| 435 | |
| 436 | arvo_class = class_create(THIS_MODULE, "arvo"); |
| 437 | if (IS_ERR(arvo_class)) |
| 438 | return PTR_ERR(arvo_class); |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 439 | arvo_class->dev_groups = arvo_groups; |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 440 | |
| 441 | retval = hid_register_driver(&arvo_driver); |
| 442 | if (retval) |
| 443 | class_destroy(arvo_class); |
| 444 | return retval; |
| 445 | } |
| 446 | |
| 447 | static void __exit arvo_exit(void) |
| 448 | { |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 449 | hid_unregister_driver(&arvo_driver); |
Stefan Achatz | 74b643d | 2011-01-30 13:38:27 +0100 | [diff] [blame] | 450 | class_destroy(arvo_class); |
Stefan Achatz | e68cc603 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | module_init(arvo_init); |
| 454 | module_exit(arvo_exit); |
| 455 | |
| 456 | MODULE_AUTHOR("Stefan Achatz"); |
| 457 | MODULE_DESCRIPTION("USB Roccat Arvo driver"); |
| 458 | MODULE_LICENSE("GPL v2"); |