Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Roccat Arvo driver for Linux |
| 4 | * |
| 5 | * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net> |
| 6 | */ |
| 7 | |
| 8 | /* |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | /* |
| 12 | * Roccat Arvo is a gamer keyboard with 5 macro keys that can be configured in |
| 13 | * 5 profiles. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/input.h> |
| 18 | #include <linux/hid.h> |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 19 | #include <linux/module.h> |
| 20 | #include <linux/slab.h> |
Stefan Achatz | 5dc0c98 | 2011-02-03 16:14:43 +0100 | [diff] [blame] | 21 | #include <linux/hid-roccat.h> |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 22 | #include "hid-ids.h" |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 23 | #include "hid-roccat-common.h" |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 24 | #include "hid-roccat-arvo.h" |
| 25 | |
| 26 | static struct class *arvo_class; |
| 27 | |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 28 | static ssize_t arvo_sysfs_show_mode_key(struct device *dev, |
| 29 | struct device_attribute *attr, char *buf) |
| 30 | { |
| 31 | struct arvo_device *arvo = |
| 32 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 33 | struct usb_device *usb_dev = |
| 34 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 35 | struct arvo_mode_key temp_buf; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 36 | int retval; |
| 37 | |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 38 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 39 | retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_MODE_KEY, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 40 | &temp_buf, sizeof(struct arvo_mode_key)); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 41 | mutex_unlock(&arvo->arvo_lock); |
| 42 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 43 | return retval; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 44 | |
Jiapeng Chong | 2fad0ab | 2021-02-02 17:31:01 +0800 | [diff] [blame] | 45 | return sysfs_emit(buf, "%d\n", temp_buf.state); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static ssize_t arvo_sysfs_set_mode_key(struct device *dev, |
| 49 | struct device_attribute *attr, char const *buf, size_t size) |
| 50 | { |
| 51 | struct arvo_device *arvo = |
| 52 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 53 | struct usb_device *usb_dev = |
| 54 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 55 | struct arvo_mode_key temp_buf; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 56 | unsigned long state; |
| 57 | int retval; |
| 58 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 59 | retval = kstrtoul(buf, 10, &state); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 60 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 61 | return retval; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 62 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 63 | temp_buf.command = ARVO_COMMAND_MODE_KEY; |
| 64 | temp_buf.state = state; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 65 | |
| 66 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 67 | retval = roccat_common2_send(usb_dev, ARVO_COMMAND_MODE_KEY, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 68 | &temp_buf, sizeof(struct arvo_mode_key)); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 69 | mutex_unlock(&arvo->arvo_lock); |
| 70 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 71 | return retval; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 72 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 73 | return size; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 74 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 75 | static DEVICE_ATTR(mode_key, 0660, |
| 76 | arvo_sysfs_show_mode_key, arvo_sysfs_set_mode_key); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 77 | |
| 78 | static ssize_t arvo_sysfs_show_key_mask(struct device *dev, |
| 79 | struct device_attribute *attr, char *buf) |
| 80 | { |
| 81 | struct arvo_device *arvo = |
| 82 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 83 | struct usb_device *usb_dev = |
| 84 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 85 | struct arvo_key_mask temp_buf; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 86 | int retval; |
| 87 | |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 88 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 89 | retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_KEY_MASK, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 90 | &temp_buf, sizeof(struct arvo_key_mask)); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 91 | mutex_unlock(&arvo->arvo_lock); |
| 92 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 93 | return retval; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 94 | |
Jiapeng Chong | 2fad0ab | 2021-02-02 17:31:01 +0800 | [diff] [blame] | 95 | return sysfs_emit(buf, "%d\n", temp_buf.key_mask); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static ssize_t arvo_sysfs_set_key_mask(struct device *dev, |
| 99 | struct device_attribute *attr, char const *buf, size_t size) |
| 100 | { |
| 101 | struct arvo_device *arvo = |
| 102 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 103 | struct usb_device *usb_dev = |
| 104 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 105 | struct arvo_key_mask temp_buf; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 106 | unsigned long key_mask; |
| 107 | int retval; |
| 108 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 109 | retval = kstrtoul(buf, 10, &key_mask); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 110 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 111 | return retval; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 112 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 113 | temp_buf.command = ARVO_COMMAND_KEY_MASK; |
| 114 | temp_buf.key_mask = key_mask; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 115 | |
| 116 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 117 | retval = roccat_common2_send(usb_dev, ARVO_COMMAND_KEY_MASK, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 118 | &temp_buf, sizeof(struct arvo_key_mask)); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 119 | mutex_unlock(&arvo->arvo_lock); |
| 120 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 121 | return retval; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 122 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 123 | return size; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 124 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 125 | static DEVICE_ATTR(key_mask, 0660, |
| 126 | arvo_sysfs_show_key_mask, arvo_sysfs_set_key_mask); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 127 | |
| 128 | /* retval is 1-5 on success, < 0 on error */ |
| 129 | static int arvo_get_actual_profile(struct usb_device *usb_dev) |
| 130 | { |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 131 | struct arvo_actual_profile temp_buf; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 132 | int retval; |
| 133 | |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 134 | retval = roccat_common2_receive(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 135 | &temp_buf, sizeof(struct arvo_actual_profile)); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 136 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 137 | if (retval) |
| 138 | return retval; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 139 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 140 | return temp_buf.actual_profile; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static ssize_t arvo_sysfs_show_actual_profile(struct device *dev, |
| 144 | struct device_attribute *attr, char *buf) |
| 145 | { |
| 146 | struct arvo_device *arvo = |
| 147 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 148 | |
Jiapeng Chong | 2fad0ab | 2021-02-02 17:31:01 +0800 | [diff] [blame] | 149 | return sysfs_emit(buf, "%d\n", arvo->actual_profile); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | static ssize_t arvo_sysfs_set_actual_profile(struct device *dev, |
| 153 | struct device_attribute *attr, char const *buf, size_t size) |
| 154 | { |
| 155 | struct arvo_device *arvo = |
| 156 | hid_get_drvdata(dev_get_drvdata(dev->parent->parent)); |
| 157 | struct usb_device *usb_dev = |
| 158 | interface_to_usbdev(to_usb_interface(dev->parent->parent)); |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 159 | struct arvo_actual_profile temp_buf; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 160 | unsigned long profile; |
| 161 | int retval; |
| 162 | |
Jingoo Han | dfc450b | 2013-07-19 15:53:16 +0900 | [diff] [blame] | 163 | retval = kstrtoul(buf, 10, &profile); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 164 | if (retval) |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 165 | return retval; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 166 | |
Stefan Achatz | 901e64d | 2011-06-12 10:02:44 +0200 | [diff] [blame] | 167 | if (profile < 1 || profile > 5) |
| 168 | return -EINVAL; |
| 169 | |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 170 | temp_buf.command = ARVO_COMMAND_ACTUAL_PROFILE; |
| 171 | temp_buf.actual_profile = profile; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 172 | |
| 173 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 174 | retval = roccat_common2_send(usb_dev, ARVO_COMMAND_ACTUAL_PROFILE, |
Stefan Achatz | 5772f63 | 2011-01-30 13:38:23 +0100 | [diff] [blame] | 175 | &temp_buf, sizeof(struct arvo_actual_profile)); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 176 | if (!retval) { |
| 177 | arvo->actual_profile = profile; |
| 178 | retval = size; |
| 179 | } |
| 180 | mutex_unlock(&arvo->arvo_lock); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 181 | return retval; |
| 182 | } |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 183 | static DEVICE_ATTR(actual_profile, 0660, |
| 184 | arvo_sysfs_show_actual_profile, |
| 185 | arvo_sysfs_set_actual_profile); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 186 | |
| 187 | static ssize_t arvo_sysfs_write(struct file *fp, |
| 188 | struct kobject *kobj, void const *buf, |
| 189 | loff_t off, size_t count, size_t real_size, uint command) |
| 190 | { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 191 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 192 | struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev)); |
| 193 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 194 | int retval; |
| 195 | |
| 196 | if (off != 0 || count != real_size) |
| 197 | return -EINVAL; |
| 198 | |
| 199 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 200 | retval = roccat_common2_send(usb_dev, command, buf, real_size); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 201 | mutex_unlock(&arvo->arvo_lock); |
| 202 | |
| 203 | return (retval ? retval : real_size); |
| 204 | } |
| 205 | |
| 206 | static ssize_t arvo_sysfs_read(struct file *fp, |
| 207 | struct kobject *kobj, void *buf, loff_t off, |
| 208 | size_t count, size_t real_size, uint command) |
| 209 | { |
Geliang Tang | 2cf8383 | 2015-12-27 17:25:24 +0800 | [diff] [blame] | 210 | struct device *dev = kobj_to_dev(kobj)->parent->parent; |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 211 | struct arvo_device *arvo = hid_get_drvdata(dev_get_drvdata(dev)); |
| 212 | struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev)); |
| 213 | int retval; |
| 214 | |
| 215 | if (off >= real_size) |
| 216 | return 0; |
| 217 | |
| 218 | if (off != 0 || count != real_size) |
| 219 | return -EINVAL; |
| 220 | |
| 221 | mutex_lock(&arvo->arvo_lock); |
Stefan Achatz | 7392d73 | 2012-05-20 22:45:04 +0200 | [diff] [blame] | 222 | retval = roccat_common2_receive(usb_dev, command, buf, real_size); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 223 | mutex_unlock(&arvo->arvo_lock); |
| 224 | |
| 225 | return (retval ? retval : real_size); |
| 226 | } |
| 227 | |
| 228 | static ssize_t arvo_sysfs_write_button(struct file *fp, |
| 229 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 230 | loff_t off, size_t count) |
| 231 | { |
| 232 | return arvo_sysfs_write(fp, kobj, buf, off, count, |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 233 | sizeof(struct arvo_button), ARVO_COMMAND_BUTTON); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 234 | } |
Greg Kroah-Hartman | e0a00d8 | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 235 | static BIN_ATTR(button, 0220, NULL, arvo_sysfs_write_button, |
| 236 | sizeof(struct arvo_button)); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 237 | |
| 238 | static ssize_t arvo_sysfs_read_info(struct file *fp, |
| 239 | struct kobject *kobj, struct bin_attribute *attr, char *buf, |
| 240 | loff_t off, size_t count) |
| 241 | { |
| 242 | return arvo_sysfs_read(fp, kobj, buf, off, count, |
Stefan Achatz | 1edd5b4 | 2011-06-01 15:54:17 +0200 | [diff] [blame] | 243 | sizeof(struct arvo_info), ARVO_COMMAND_INFO); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 244 | } |
Greg Kroah-Hartman | e0a00d8 | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 245 | static BIN_ATTR(info, 0440, arvo_sysfs_read_info, NULL, |
| 246 | sizeof(struct arvo_info)); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 247 | |
Greg Kroah-Hartman | 46a58c4 | 2013-07-24 15:05:11 -0700 | [diff] [blame] | 248 | static struct attribute *arvo_attrs[] = { |
| 249 | &dev_attr_mode_key.attr, |
| 250 | &dev_attr_key_mask.attr, |
| 251 | &dev_attr_actual_profile.attr, |
| 252 | NULL, |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 253 | }; |
| 254 | |
Greg Kroah-Hartman | e0a00d8 | 2013-08-19 21:40:26 -0700 | [diff] [blame] | 255 | static struct bin_attribute *arvo_bin_attributes[] = { |
| 256 | &bin_attr_button, |
| 257 | &bin_attr_info, |
| 258 | NULL, |
| 259 | }; |
| 260 | |
| 261 | static const struct attribute_group arvo_group = { |
| 262 | .attrs = arvo_attrs, |
| 263 | .bin_attrs = arvo_bin_attributes, |
| 264 | }; |
| 265 | |
| 266 | static const struct attribute_group *arvo_groups[] = { |
| 267 | &arvo_group, |
| 268 | NULL, |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | static int arvo_init_arvo_device_struct(struct usb_device *usb_dev, |
| 272 | struct arvo_device *arvo) |
| 273 | { |
| 274 | int retval; |
| 275 | |
| 276 | mutex_init(&arvo->arvo_lock); |
| 277 | |
| 278 | retval = arvo_get_actual_profile(usb_dev); |
| 279 | if (retval < 0) |
| 280 | return retval; |
| 281 | arvo->actual_profile = retval; |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int arvo_init_specials(struct hid_device *hdev) |
| 287 | { |
| 288 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 289 | struct usb_device *usb_dev = interface_to_usbdev(intf); |
| 290 | struct arvo_device *arvo; |
| 291 | int retval; |
| 292 | |
| 293 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 294 | == USB_INTERFACE_PROTOCOL_KEYBOARD) { |
| 295 | hid_set_drvdata(hdev, NULL); |
| 296 | return 0; |
| 297 | } |
| 298 | |
| 299 | arvo = kzalloc(sizeof(*arvo), GFP_KERNEL); |
| 300 | if (!arvo) { |
Stefan Achatz | a28764e | 2011-01-30 13:38:21 +0100 | [diff] [blame] | 301 | hid_err(hdev, "can't alloc device descriptor\n"); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 302 | return -ENOMEM; |
| 303 | } |
| 304 | hid_set_drvdata(hdev, arvo); |
| 305 | |
| 306 | retval = arvo_init_arvo_device_struct(usb_dev, arvo); |
| 307 | if (retval) { |
Stefan Achatz | a28764e | 2011-01-30 13:38:21 +0100 | [diff] [blame] | 308 | hid_err(hdev, "couldn't init struct arvo_device\n"); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 309 | goto exit_free; |
| 310 | } |
| 311 | |
Stefan Achatz | 8211e46 | 2011-01-30 13:38:25 +0100 | [diff] [blame] | 312 | retval = roccat_connect(arvo_class, hdev, |
| 313 | sizeof(struct arvo_roccat_report)); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 314 | if (retval < 0) { |
Stefan Achatz | a28764e | 2011-01-30 13:38:21 +0100 | [diff] [blame] | 315 | hid_err(hdev, "couldn't init char dev\n"); |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 316 | } else { |
| 317 | arvo->chrdev_minor = retval; |
| 318 | arvo->roccat_claimed = 1; |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | exit_free: |
| 323 | kfree(arvo); |
| 324 | return retval; |
| 325 | } |
| 326 | |
| 327 | static void arvo_remove_specials(struct hid_device *hdev) |
| 328 | { |
| 329 | struct usb_interface *intf = to_usb_interface(hdev->dev.parent); |
| 330 | struct arvo_device *arvo; |
| 331 | |
| 332 | if (intf->cur_altsetting->desc.bInterfaceProtocol |
| 333 | == USB_INTERFACE_PROTOCOL_KEYBOARD) |
| 334 | return; |
| 335 | |
| 336 | arvo = hid_get_drvdata(hdev); |
| 337 | if (arvo->roccat_claimed) |
| 338 | roccat_disconnect(arvo->chrdev_minor); |
| 339 | kfree(arvo); |
| 340 | } |
| 341 | |
| 342 | static int arvo_probe(struct hid_device *hdev, |
| 343 | const struct hid_device_id *id) |
| 344 | { |
| 345 | int retval; |
| 346 | |
Greg Kroah-Hartman | 9302095 | 2021-12-01 19:35:03 +0100 | [diff] [blame] | 347 | if (!hid_is_usb(hdev)) |
| 348 | return -EINVAL; |
| 349 | |
Stefan Achatz | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 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 | e68cc60 | 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 | e68cc60 | 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 | e68cc60 | 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 | e68cc60 | 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 | e68cc60 | 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 | e68cc60 | 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 | e68cc60 | 2011-01-06 09:00:34 +0100 | [diff] [blame] | 449 | hid_unregister_driver(&arvo_driver); |
Stefan Achatz | 74b643da | 2011-01-30 13:38:27 +0100 | [diff] [blame] | 450 | class_destroy(arvo_class); |
Stefan Achatz | e68cc60 | 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"); |