1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Roccat Isku driver for Linux
5 * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
12 * Roccat Isku is a gamer keyboard with macro keys that can be configured in
16 #include <linux/device.h>
17 #include <linux/input.h>
18 #include <linux/hid.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/hid-roccat.h>
23 #include "hid-roccat-common.h"
24 #include "hid-roccat-isku.h"
26 static void isku_profile_activated(struct isku_device *isku, uint new_profile)
28 isku->actual_profile = new_profile;
31 static int isku_receive(struct usb_device *usb_dev, uint command,
34 return roccat_common2_receive(usb_dev, command, buf, size);
37 static int isku_get_actual_profile(struct usb_device *usb_dev)
39 struct isku_actual_profile buf;
42 retval = isku_receive(usb_dev, ISKU_COMMAND_ACTUAL_PROFILE,
43 &buf, sizeof(struct isku_actual_profile));
44 return retval ? retval : buf.actual_profile;
47 static int isku_set_actual_profile(struct usb_device *usb_dev, int new_profile)
49 struct isku_actual_profile buf;
51 buf.command = ISKU_COMMAND_ACTUAL_PROFILE;
52 buf.size = sizeof(struct isku_actual_profile);
53 buf.actual_profile = new_profile;
54 return roccat_common2_send_with_status(usb_dev,
55 ISKU_COMMAND_ACTUAL_PROFILE, &buf,
56 sizeof(struct isku_actual_profile));
59 static ssize_t isku_sysfs_show_actual_profile(struct device *dev,
60 struct device_attribute *attr, char *buf)
62 struct isku_device *isku =
63 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
64 return snprintf(buf, PAGE_SIZE, "%d\n", isku->actual_profile);
67 static ssize_t isku_sysfs_set_actual_profile(struct device *dev,
68 struct device_attribute *attr, char const *buf, size_t size)
70 struct isku_device *isku;
71 struct usb_device *usb_dev;
72 unsigned long profile;
74 struct isku_roccat_report roccat_report;
76 dev = dev->parent->parent;
77 isku = hid_get_drvdata(dev_get_drvdata(dev));
78 usb_dev = interface_to_usbdev(to_usb_interface(dev));
80 retval = kstrtoul(buf, 10, &profile);
87 mutex_lock(&isku->isku_lock);
89 retval = isku_set_actual_profile(usb_dev, profile);
91 mutex_unlock(&isku->isku_lock);
95 isku_profile_activated(isku, profile);
97 roccat_report.event = ISKU_REPORT_BUTTON_EVENT_PROFILE;
98 roccat_report.data1 = profile + 1;
99 roccat_report.data2 = 0;
100 roccat_report.profile = profile + 1;
101 roccat_report_event(isku->chrdev_minor, (uint8_t const *)&roccat_report);
103 mutex_unlock(&isku->isku_lock);
107 static DEVICE_ATTR(actual_profile, 0660, isku_sysfs_show_actual_profile,
108 isku_sysfs_set_actual_profile);
110 static struct attribute *isku_attrs[] = {
111 &dev_attr_actual_profile.attr,
115 static ssize_t isku_sysfs_read(struct file *fp, struct kobject *kobj,
116 char *buf, loff_t off, size_t count,
117 size_t real_size, uint command)
119 struct device *dev = kobj_to_dev(kobj)->parent->parent;
120 struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
121 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
124 if (off >= real_size)
127 if (off != 0 || count > real_size)
130 mutex_lock(&isku->isku_lock);
131 retval = isku_receive(usb_dev, command, buf, count);
132 mutex_unlock(&isku->isku_lock);
134 return retval ? retval : count;
137 static ssize_t isku_sysfs_write(struct file *fp, struct kobject *kobj,
138 void const *buf, loff_t off, size_t count,
139 size_t real_size, uint command)
141 struct device *dev = kobj_to_dev(kobj)->parent->parent;
142 struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
143 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
146 if (off != 0 || count > real_size)
149 mutex_lock(&isku->isku_lock);
150 retval = roccat_common2_send_with_status(usb_dev, command,
152 mutex_unlock(&isku->isku_lock);
154 return retval ? retval : count;
157 #define ISKU_SYSFS_W(thingy, THINGY) \
158 static ssize_t isku_sysfs_write_ ## thingy(struct file *fp, struct kobject *kobj, \
159 struct bin_attribute *attr, char *buf, \
160 loff_t off, size_t count) \
162 return isku_sysfs_write(fp, kobj, buf, off, count, \
163 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
166 #define ISKU_SYSFS_R(thingy, THINGY) \
167 static ssize_t isku_sysfs_read_ ## thingy(struct file *fp, struct kobject *kobj, \
168 struct bin_attribute *attr, char *buf, \
169 loff_t off, size_t count) \
171 return isku_sysfs_read(fp, kobj, buf, off, count, \
172 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
175 #define ISKU_SYSFS_RW(thingy, THINGY) \
176 ISKU_SYSFS_R(thingy, THINGY) \
177 ISKU_SYSFS_W(thingy, THINGY)
179 #define ISKU_BIN_ATTR_RW(thingy, THINGY) \
180 ISKU_SYSFS_RW(thingy, THINGY); \
181 static struct bin_attribute bin_attr_##thingy = { \
182 .attr = { .name = #thingy, .mode = 0660 }, \
183 .size = ISKU_SIZE_ ## THINGY, \
184 .read = isku_sysfs_read_ ## thingy, \
185 .write = isku_sysfs_write_ ## thingy \
188 #define ISKU_BIN_ATTR_R(thingy, THINGY) \
189 ISKU_SYSFS_R(thingy, THINGY); \
190 static struct bin_attribute bin_attr_##thingy = { \
191 .attr = { .name = #thingy, .mode = 0440 }, \
192 .size = ISKU_SIZE_ ## THINGY, \
193 .read = isku_sysfs_read_ ## thingy, \
196 #define ISKU_BIN_ATTR_W(thingy, THINGY) \
197 ISKU_SYSFS_W(thingy, THINGY); \
198 static struct bin_attribute bin_attr_##thingy = { \
199 .attr = { .name = #thingy, .mode = 0220 }, \
200 .size = ISKU_SIZE_ ## THINGY, \
201 .write = isku_sysfs_write_ ## thingy \
204 ISKU_BIN_ATTR_RW(macro, MACRO);
205 ISKU_BIN_ATTR_RW(keys_function, KEYS_FUNCTION);
206 ISKU_BIN_ATTR_RW(keys_easyzone, KEYS_EASYZONE);
207 ISKU_BIN_ATTR_RW(keys_media, KEYS_MEDIA);
208 ISKU_BIN_ATTR_RW(keys_thumbster, KEYS_THUMBSTER);
209 ISKU_BIN_ATTR_RW(keys_macro, KEYS_MACRO);
210 ISKU_BIN_ATTR_RW(keys_capslock, KEYS_CAPSLOCK);
211 ISKU_BIN_ATTR_RW(light, LIGHT);
212 ISKU_BIN_ATTR_RW(key_mask, KEY_MASK);
213 ISKU_BIN_ATTR_RW(last_set, LAST_SET);
214 ISKU_BIN_ATTR_W(talk, TALK);
215 ISKU_BIN_ATTR_W(talkfx, TALKFX);
216 ISKU_BIN_ATTR_W(control, CONTROL);
217 ISKU_BIN_ATTR_W(reset, RESET);
218 ISKU_BIN_ATTR_R(info, INFO);
220 static struct bin_attribute *isku_bin_attributes[] = {
222 &bin_attr_keys_function,
223 &bin_attr_keys_easyzone,
224 &bin_attr_keys_media,
225 &bin_attr_keys_thumbster,
226 &bin_attr_keys_macro,
227 &bin_attr_keys_capslock,
239 static const struct attribute_group isku_group = {
241 .bin_attrs = isku_bin_attributes,
244 static const struct attribute_group *isku_groups[] = {
249 static const struct class isku_class = {
251 .dev_groups = isku_groups,
254 static int isku_init_isku_device_struct(struct usb_device *usb_dev,
255 struct isku_device *isku)
259 mutex_init(&isku->isku_lock);
261 retval = isku_get_actual_profile(usb_dev);
264 isku_profile_activated(isku, retval);
269 static int isku_init_specials(struct hid_device *hdev)
271 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
272 struct usb_device *usb_dev = interface_to_usbdev(intf);
273 struct isku_device *isku;
276 if (intf->cur_altsetting->desc.bInterfaceProtocol
277 != ISKU_USB_INTERFACE_PROTOCOL) {
278 hid_set_drvdata(hdev, NULL);
282 isku = kzalloc(sizeof(*isku), GFP_KERNEL);
284 hid_err(hdev, "can't alloc device descriptor\n");
287 hid_set_drvdata(hdev, isku);
289 retval = isku_init_isku_device_struct(usb_dev, isku);
291 hid_err(hdev, "couldn't init struct isku_device\n");
295 retval = roccat_connect(&isku_class, hdev,
296 sizeof(struct isku_roccat_report));
298 hid_err(hdev, "couldn't init char dev\n");
300 isku->chrdev_minor = retval;
301 isku->roccat_claimed = 1;
310 static void isku_remove_specials(struct hid_device *hdev)
312 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
313 struct isku_device *isku;
315 if (intf->cur_altsetting->desc.bInterfaceProtocol
316 != ISKU_USB_INTERFACE_PROTOCOL)
319 isku = hid_get_drvdata(hdev);
320 if (isku->roccat_claimed)
321 roccat_disconnect(isku->chrdev_minor);
325 static int isku_probe(struct hid_device *hdev,
326 const struct hid_device_id *id)
330 if (!hid_is_usb(hdev))
333 retval = hid_parse(hdev);
335 hid_err(hdev, "parse failed\n");
339 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
341 hid_err(hdev, "hw start failed\n");
345 retval = isku_init_specials(hdev);
347 hid_err(hdev, "couldn't install keyboard\n");
359 static void isku_remove(struct hid_device *hdev)
361 isku_remove_specials(hdev);
365 static void isku_keep_values_up_to_date(struct isku_device *isku,
368 struct isku_report_button const *button_report;
371 case ISKU_REPORT_NUMBER_BUTTON:
372 button_report = (struct isku_report_button const *)data;
373 switch (button_report->event) {
374 case ISKU_REPORT_BUTTON_EVENT_PROFILE:
375 isku_profile_activated(isku, button_report->data1 - 1);
382 static void isku_report_to_chrdev(struct isku_device const *isku,
385 struct isku_roccat_report roccat_report;
386 struct isku_report_button const *button_report;
388 if (data[0] != ISKU_REPORT_NUMBER_BUTTON)
391 button_report = (struct isku_report_button const *)data;
393 roccat_report.event = button_report->event;
394 roccat_report.data1 = button_report->data1;
395 roccat_report.data2 = button_report->data2;
396 roccat_report.profile = isku->actual_profile + 1;
397 roccat_report_event(isku->chrdev_minor,
398 (uint8_t const *)&roccat_report);
401 static int isku_raw_event(struct hid_device *hdev,
402 struct hid_report *report, u8 *data, int size)
404 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
405 struct isku_device *isku = hid_get_drvdata(hdev);
407 if (intf->cur_altsetting->desc.bInterfaceProtocol
408 != ISKU_USB_INTERFACE_PROTOCOL)
414 isku_keep_values_up_to_date(isku, data);
416 if (isku->roccat_claimed)
417 isku_report_to_chrdev(isku, data);
422 static const struct hid_device_id isku_devices[] = {
423 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
424 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKUFX) },
428 MODULE_DEVICE_TABLE(hid, isku_devices);
430 static struct hid_driver isku_driver = {
432 .id_table = isku_devices,
434 .remove = isku_remove,
435 .raw_event = isku_raw_event
438 static int __init isku_init(void)
442 retval = class_register(&isku_class);
446 retval = hid_register_driver(&isku_driver);
448 class_unregister(&isku_class);
452 static void __exit isku_exit(void)
454 hid_unregister_driver(&isku_driver);
455 class_unregister(&isku_class);
458 module_init(isku_init);
459 module_exit(isku_exit);
461 MODULE_AUTHOR("Stefan Achatz");
462 MODULE_DESCRIPTION("USB Roccat Isku/FX driver");
463 MODULE_LICENSE("GPL v2");