Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Roccat Isku driver for Linux
0004  *
0005  * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
0006  */
0007 
0008 /*
0009  */
0010 
0011 /*
0012  * Roccat Isku is a gamer keyboard with macro keys that can be configured in
0013  * 5 profiles.
0014  */
0015 
0016 #include <linux/device.h>
0017 #include <linux/input.h>
0018 #include <linux/hid.h>
0019 #include <linux/module.h>
0020 #include <linux/slab.h>
0021 #include <linux/hid-roccat.h>
0022 #include "hid-ids.h"
0023 #include "hid-roccat-common.h"
0024 #include "hid-roccat-isku.h"
0025 
0026 static struct class *isku_class;
0027 
0028 static void isku_profile_activated(struct isku_device *isku, uint new_profile)
0029 {
0030     isku->actual_profile = new_profile;
0031 }
0032 
0033 static int isku_receive(struct usb_device *usb_dev, uint command,
0034         void *buf, uint size)
0035 {
0036     return roccat_common2_receive(usb_dev, command, buf, size);
0037 }
0038 
0039 static int isku_get_actual_profile(struct usb_device *usb_dev)
0040 {
0041     struct isku_actual_profile buf;
0042     int retval;
0043 
0044     retval = isku_receive(usb_dev, ISKU_COMMAND_ACTUAL_PROFILE,
0045             &buf, sizeof(struct isku_actual_profile));
0046     return retval ? retval : buf.actual_profile;
0047 }
0048 
0049 static int isku_set_actual_profile(struct usb_device *usb_dev, int new_profile)
0050 {
0051     struct isku_actual_profile buf;
0052 
0053     buf.command = ISKU_COMMAND_ACTUAL_PROFILE;
0054     buf.size = sizeof(struct isku_actual_profile);
0055     buf.actual_profile = new_profile;
0056     return roccat_common2_send_with_status(usb_dev,
0057             ISKU_COMMAND_ACTUAL_PROFILE, &buf,
0058             sizeof(struct isku_actual_profile));
0059 }
0060 
0061 static ssize_t isku_sysfs_show_actual_profile(struct device *dev,
0062         struct device_attribute *attr, char *buf)
0063 {
0064     struct isku_device *isku =
0065             hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
0066     return snprintf(buf, PAGE_SIZE, "%d\n", isku->actual_profile);
0067 }
0068 
0069 static ssize_t isku_sysfs_set_actual_profile(struct device *dev,
0070         struct device_attribute *attr, char const *buf, size_t size)
0071 {
0072     struct isku_device *isku;
0073     struct usb_device *usb_dev;
0074     unsigned long profile;
0075     int retval;
0076     struct isku_roccat_report roccat_report;
0077 
0078     dev = dev->parent->parent;
0079     isku = hid_get_drvdata(dev_get_drvdata(dev));
0080     usb_dev = interface_to_usbdev(to_usb_interface(dev));
0081 
0082     retval = kstrtoul(buf, 10, &profile);
0083     if (retval)
0084         return retval;
0085 
0086     if (profile > 4)
0087         return -EINVAL;
0088 
0089     mutex_lock(&isku->isku_lock);
0090 
0091     retval = isku_set_actual_profile(usb_dev, profile);
0092     if (retval) {
0093         mutex_unlock(&isku->isku_lock);
0094         return retval;
0095     }
0096 
0097     isku_profile_activated(isku, profile);
0098 
0099     roccat_report.event = ISKU_REPORT_BUTTON_EVENT_PROFILE;
0100     roccat_report.data1 = profile + 1;
0101     roccat_report.data2 = 0;
0102     roccat_report.profile = profile + 1;
0103     roccat_report_event(isku->chrdev_minor, (uint8_t const *)&roccat_report);
0104 
0105     mutex_unlock(&isku->isku_lock);
0106 
0107     return size;
0108 }
0109 static DEVICE_ATTR(actual_profile, 0660, isku_sysfs_show_actual_profile,
0110            isku_sysfs_set_actual_profile);
0111 
0112 static struct attribute *isku_attrs[] = {
0113     &dev_attr_actual_profile.attr,
0114     NULL,
0115 };
0116 
0117 static ssize_t isku_sysfs_read(struct file *fp, struct kobject *kobj,
0118         char *buf, loff_t off, size_t count,
0119         size_t real_size, uint command)
0120 {
0121     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0122     struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
0123     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0124     int retval;
0125 
0126     if (off >= real_size)
0127         return 0;
0128 
0129     if (off != 0 || count > real_size)
0130         return -EINVAL;
0131 
0132     mutex_lock(&isku->isku_lock);
0133     retval = isku_receive(usb_dev, command, buf, count);
0134     mutex_unlock(&isku->isku_lock);
0135 
0136     return retval ? retval : count;
0137 }
0138 
0139 static ssize_t isku_sysfs_write(struct file *fp, struct kobject *kobj,
0140         void const *buf, loff_t off, size_t count,
0141         size_t real_size, uint command)
0142 {
0143     struct device *dev = kobj_to_dev(kobj)->parent->parent;
0144     struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
0145     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0146     int retval;
0147 
0148     if (off != 0 || count > real_size)
0149         return -EINVAL;
0150 
0151     mutex_lock(&isku->isku_lock);
0152     retval = roccat_common2_send_with_status(usb_dev, command,
0153             (void *)buf, count);
0154     mutex_unlock(&isku->isku_lock);
0155 
0156     return retval ? retval : count;
0157 }
0158 
0159 #define ISKU_SYSFS_W(thingy, THINGY) \
0160 static ssize_t isku_sysfs_write_ ## thingy(struct file *fp, struct kobject *kobj, \
0161         struct bin_attribute *attr, char *buf, \
0162         loff_t off, size_t count) \
0163 { \
0164     return isku_sysfs_write(fp, kobj, buf, off, count, \
0165             ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
0166 }
0167 
0168 #define ISKU_SYSFS_R(thingy, THINGY) \
0169 static ssize_t isku_sysfs_read_ ## thingy(struct file *fp, struct kobject *kobj, \
0170         struct bin_attribute *attr, char *buf, \
0171         loff_t off, size_t count) \
0172 { \
0173     return isku_sysfs_read(fp, kobj, buf, off, count, \
0174             ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
0175 }
0176 
0177 #define ISKU_SYSFS_RW(thingy, THINGY) \
0178 ISKU_SYSFS_R(thingy, THINGY) \
0179 ISKU_SYSFS_W(thingy, THINGY)
0180 
0181 #define ISKU_BIN_ATTR_RW(thingy, THINGY) \
0182 ISKU_SYSFS_RW(thingy, THINGY); \
0183 static struct bin_attribute bin_attr_##thingy = { \
0184     .attr = { .name = #thingy, .mode = 0660 }, \
0185     .size = ISKU_SIZE_ ## THINGY, \
0186     .read = isku_sysfs_read_ ## thingy, \
0187     .write = isku_sysfs_write_ ## thingy \
0188 }
0189 
0190 #define ISKU_BIN_ATTR_R(thingy, THINGY) \
0191 ISKU_SYSFS_R(thingy, THINGY); \
0192 static struct bin_attribute bin_attr_##thingy = { \
0193     .attr = { .name = #thingy, .mode = 0440 }, \
0194     .size = ISKU_SIZE_ ## THINGY, \
0195     .read = isku_sysfs_read_ ## thingy, \
0196 }
0197 
0198 #define ISKU_BIN_ATTR_W(thingy, THINGY) \
0199 ISKU_SYSFS_W(thingy, THINGY); \
0200 static struct bin_attribute bin_attr_##thingy = { \
0201     .attr = { .name = #thingy, .mode = 0220 }, \
0202     .size = ISKU_SIZE_ ## THINGY, \
0203     .write = isku_sysfs_write_ ## thingy \
0204 }
0205 
0206 ISKU_BIN_ATTR_RW(macro, MACRO);
0207 ISKU_BIN_ATTR_RW(keys_function, KEYS_FUNCTION);
0208 ISKU_BIN_ATTR_RW(keys_easyzone, KEYS_EASYZONE);
0209 ISKU_BIN_ATTR_RW(keys_media, KEYS_MEDIA);
0210 ISKU_BIN_ATTR_RW(keys_thumbster, KEYS_THUMBSTER);
0211 ISKU_BIN_ATTR_RW(keys_macro, KEYS_MACRO);
0212 ISKU_BIN_ATTR_RW(keys_capslock, KEYS_CAPSLOCK);
0213 ISKU_BIN_ATTR_RW(light, LIGHT);
0214 ISKU_BIN_ATTR_RW(key_mask, KEY_MASK);
0215 ISKU_BIN_ATTR_RW(last_set, LAST_SET);
0216 ISKU_BIN_ATTR_W(talk, TALK);
0217 ISKU_BIN_ATTR_W(talkfx, TALKFX);
0218 ISKU_BIN_ATTR_W(control, CONTROL);
0219 ISKU_BIN_ATTR_W(reset, RESET);
0220 ISKU_BIN_ATTR_R(info, INFO);
0221 
0222 static struct bin_attribute *isku_bin_attributes[] = {
0223     &bin_attr_macro,
0224     &bin_attr_keys_function,
0225     &bin_attr_keys_easyzone,
0226     &bin_attr_keys_media,
0227     &bin_attr_keys_thumbster,
0228     &bin_attr_keys_macro,
0229     &bin_attr_keys_capslock,
0230     &bin_attr_light,
0231     &bin_attr_key_mask,
0232     &bin_attr_last_set,
0233     &bin_attr_talk,
0234     &bin_attr_talkfx,
0235     &bin_attr_control,
0236     &bin_attr_reset,
0237     &bin_attr_info,
0238     NULL,
0239 };
0240 
0241 static const struct attribute_group isku_group = {
0242     .attrs = isku_attrs,
0243     .bin_attrs = isku_bin_attributes,
0244 };
0245 
0246 static const struct attribute_group *isku_groups[] = {
0247     &isku_group,
0248     NULL,
0249 };
0250 
0251 static int isku_init_isku_device_struct(struct usb_device *usb_dev,
0252         struct isku_device *isku)
0253 {
0254     int retval;
0255 
0256     mutex_init(&isku->isku_lock);
0257 
0258     retval = isku_get_actual_profile(usb_dev);
0259     if (retval < 0)
0260         return retval;
0261     isku_profile_activated(isku, retval);
0262 
0263     return 0;
0264 }
0265 
0266 static int isku_init_specials(struct hid_device *hdev)
0267 {
0268     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0269     struct usb_device *usb_dev = interface_to_usbdev(intf);
0270     struct isku_device *isku;
0271     int retval;
0272 
0273     if (intf->cur_altsetting->desc.bInterfaceProtocol
0274             != ISKU_USB_INTERFACE_PROTOCOL) {
0275         hid_set_drvdata(hdev, NULL);
0276         return 0;
0277     }
0278 
0279     isku = kzalloc(sizeof(*isku), GFP_KERNEL);
0280     if (!isku) {
0281         hid_err(hdev, "can't alloc device descriptor\n");
0282         return -ENOMEM;
0283     }
0284     hid_set_drvdata(hdev, isku);
0285 
0286     retval = isku_init_isku_device_struct(usb_dev, isku);
0287     if (retval) {
0288         hid_err(hdev, "couldn't init struct isku_device\n");
0289         goto exit_free;
0290     }
0291 
0292     retval = roccat_connect(isku_class, hdev,
0293             sizeof(struct isku_roccat_report));
0294     if (retval < 0) {
0295         hid_err(hdev, "couldn't init char dev\n");
0296     } else {
0297         isku->chrdev_minor = retval;
0298         isku->roccat_claimed = 1;
0299     }
0300 
0301     return 0;
0302 exit_free:
0303     kfree(isku);
0304     return retval;
0305 }
0306 
0307 static void isku_remove_specials(struct hid_device *hdev)
0308 {
0309     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0310     struct isku_device *isku;
0311 
0312     if (intf->cur_altsetting->desc.bInterfaceProtocol
0313             != ISKU_USB_INTERFACE_PROTOCOL)
0314         return;
0315 
0316     isku = hid_get_drvdata(hdev);
0317     if (isku->roccat_claimed)
0318         roccat_disconnect(isku->chrdev_minor);
0319     kfree(isku);
0320 }
0321 
0322 static int isku_probe(struct hid_device *hdev,
0323         const struct hid_device_id *id)
0324 {
0325     int retval;
0326 
0327     if (!hid_is_usb(hdev))
0328         return -EINVAL;
0329 
0330     retval = hid_parse(hdev);
0331     if (retval) {
0332         hid_err(hdev, "parse failed\n");
0333         goto exit;
0334     }
0335 
0336     retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0337     if (retval) {
0338         hid_err(hdev, "hw start failed\n");
0339         goto exit;
0340     }
0341 
0342     retval = isku_init_specials(hdev);
0343     if (retval) {
0344         hid_err(hdev, "couldn't install keyboard\n");
0345         goto exit_stop;
0346     }
0347 
0348     return 0;
0349 
0350 exit_stop:
0351     hid_hw_stop(hdev);
0352 exit:
0353     return retval;
0354 }
0355 
0356 static void isku_remove(struct hid_device *hdev)
0357 {
0358     isku_remove_specials(hdev);
0359     hid_hw_stop(hdev);
0360 }
0361 
0362 static void isku_keep_values_up_to_date(struct isku_device *isku,
0363         u8 const *data)
0364 {
0365     struct isku_report_button const *button_report;
0366 
0367     switch (data[0]) {
0368     case ISKU_REPORT_NUMBER_BUTTON:
0369         button_report = (struct isku_report_button const *)data;
0370         switch (button_report->event) {
0371         case ISKU_REPORT_BUTTON_EVENT_PROFILE:
0372             isku_profile_activated(isku, button_report->data1 - 1);
0373             break;
0374         }
0375         break;
0376     }
0377 }
0378 
0379 static void isku_report_to_chrdev(struct isku_device const *isku,
0380         u8 const *data)
0381 {
0382     struct isku_roccat_report roccat_report;
0383     struct isku_report_button const *button_report;
0384 
0385     if (data[0] != ISKU_REPORT_NUMBER_BUTTON)
0386         return;
0387 
0388     button_report = (struct isku_report_button const *)data;
0389 
0390     roccat_report.event = button_report->event;
0391     roccat_report.data1 = button_report->data1;
0392     roccat_report.data2 = button_report->data2;
0393     roccat_report.profile = isku->actual_profile + 1;
0394     roccat_report_event(isku->chrdev_minor,
0395             (uint8_t const *)&roccat_report);
0396 }
0397 
0398 static int isku_raw_event(struct hid_device *hdev,
0399         struct hid_report *report, u8 *data, int size)
0400 {
0401     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0402     struct isku_device *isku = hid_get_drvdata(hdev);
0403 
0404     if (intf->cur_altsetting->desc.bInterfaceProtocol
0405             != ISKU_USB_INTERFACE_PROTOCOL)
0406         return 0;
0407 
0408     if (isku == NULL)
0409         return 0;
0410 
0411     isku_keep_values_up_to_date(isku, data);
0412 
0413     if (isku->roccat_claimed)
0414         isku_report_to_chrdev(isku, data);
0415 
0416     return 0;
0417 }
0418 
0419 static const struct hid_device_id isku_devices[] = {
0420     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
0421     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKUFX) },
0422     { }
0423 };
0424 
0425 MODULE_DEVICE_TABLE(hid, isku_devices);
0426 
0427 static struct hid_driver isku_driver = {
0428         .name = "isku",
0429         .id_table = isku_devices,
0430         .probe = isku_probe,
0431         .remove = isku_remove,
0432         .raw_event = isku_raw_event
0433 };
0434 
0435 static int __init isku_init(void)
0436 {
0437     int retval;
0438     isku_class = class_create(THIS_MODULE, "isku");
0439     if (IS_ERR(isku_class))
0440         return PTR_ERR(isku_class);
0441     isku_class->dev_groups = isku_groups;
0442 
0443     retval = hid_register_driver(&isku_driver);
0444     if (retval)
0445         class_destroy(isku_class);
0446     return retval;
0447 }
0448 
0449 static void __exit isku_exit(void)
0450 {
0451     hid_unregister_driver(&isku_driver);
0452     class_destroy(isku_class);
0453 }
0454 
0455 module_init(isku_init);
0456 module_exit(isku_exit);
0457 
0458 MODULE_AUTHOR("Stefan Achatz");
0459 MODULE_DESCRIPTION("USB Roccat Isku/FX driver");
0460 MODULE_LICENSE("GPL v2");