0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <linux/device.h>
0016 #include <linux/input.h>
0017 #include <linux/hid.h>
0018 #include <linux/module.h>
0019 #include <linux/slab.h>
0020 #include <linux/hid-roccat.h>
0021 #include "hid-ids.h"
0022 #include "hid-roccat-common.h"
0023 #include "hid-roccat-savu.h"
0024
0025 static struct class *savu_class;
0026
0027 ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x4, 0x03);
0028 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x5, 0x03);
0029 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(general, 0x6, 0x10);
0030 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(buttons, 0x7, 0x2f);
0031 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x8, 0x0823);
0032 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x9, 0x08);
0033 ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0xc, 0x04);
0034
0035 static struct bin_attribute *savu_bin_attrs[] = {
0036 &bin_attr_control,
0037 &bin_attr_profile,
0038 &bin_attr_general,
0039 &bin_attr_buttons,
0040 &bin_attr_macro,
0041 &bin_attr_info,
0042 &bin_attr_sensor,
0043 NULL,
0044 };
0045
0046 static const struct attribute_group savu_group = {
0047 .bin_attrs = savu_bin_attrs,
0048 };
0049
0050 static const struct attribute_group *savu_groups[] = {
0051 &savu_group,
0052 NULL,
0053 };
0054
0055 static int savu_init_specials(struct hid_device *hdev)
0056 {
0057 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0058 struct usb_device *usb_dev = interface_to_usbdev(intf);
0059 struct roccat_common2_device *savu;
0060 int retval;
0061
0062 if (intf->cur_altsetting->desc.bInterfaceProtocol
0063 != USB_INTERFACE_PROTOCOL_MOUSE) {
0064 hid_set_drvdata(hdev, NULL);
0065 return 0;
0066 }
0067
0068 savu = kzalloc(sizeof(*savu), GFP_KERNEL);
0069 if (!savu) {
0070 hid_err(hdev, "can't alloc device descriptor\n");
0071 return -ENOMEM;
0072 }
0073 hid_set_drvdata(hdev, savu);
0074
0075 retval = roccat_common2_device_init_struct(usb_dev, savu);
0076 if (retval) {
0077 hid_err(hdev, "couldn't init Savu device\n");
0078 goto exit_free;
0079 }
0080
0081 retval = roccat_connect(savu_class, hdev,
0082 sizeof(struct savu_roccat_report));
0083 if (retval < 0) {
0084 hid_err(hdev, "couldn't init char dev\n");
0085 } else {
0086 savu->chrdev_minor = retval;
0087 savu->roccat_claimed = 1;
0088 }
0089
0090 return 0;
0091 exit_free:
0092 kfree(savu);
0093 return retval;
0094 }
0095
0096 static void savu_remove_specials(struct hid_device *hdev)
0097 {
0098 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0099 struct roccat_common2_device *savu;
0100
0101 if (intf->cur_altsetting->desc.bInterfaceProtocol
0102 != USB_INTERFACE_PROTOCOL_MOUSE)
0103 return;
0104
0105 savu = hid_get_drvdata(hdev);
0106 if (savu->roccat_claimed)
0107 roccat_disconnect(savu->chrdev_minor);
0108 kfree(savu);
0109 }
0110
0111 static int savu_probe(struct hid_device *hdev,
0112 const struct hid_device_id *id)
0113 {
0114 int retval;
0115
0116 if (!hid_is_usb(hdev))
0117 return -EINVAL;
0118
0119 retval = hid_parse(hdev);
0120 if (retval) {
0121 hid_err(hdev, "parse failed\n");
0122 goto exit;
0123 }
0124
0125 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0126 if (retval) {
0127 hid_err(hdev, "hw start failed\n");
0128 goto exit;
0129 }
0130
0131 retval = savu_init_specials(hdev);
0132 if (retval) {
0133 hid_err(hdev, "couldn't install mouse\n");
0134 goto exit_stop;
0135 }
0136
0137 return 0;
0138
0139 exit_stop:
0140 hid_hw_stop(hdev);
0141 exit:
0142 return retval;
0143 }
0144
0145 static void savu_remove(struct hid_device *hdev)
0146 {
0147 savu_remove_specials(hdev);
0148 hid_hw_stop(hdev);
0149 }
0150
0151 static void savu_report_to_chrdev(struct roccat_common2_device const *savu,
0152 u8 const *data)
0153 {
0154 struct savu_roccat_report roccat_report;
0155 struct savu_mouse_report_special const *special_report;
0156
0157 if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL)
0158 return;
0159
0160 special_report = (struct savu_mouse_report_special const *)data;
0161
0162 roccat_report.type = special_report->type;
0163 roccat_report.data[0] = special_report->data[0];
0164 roccat_report.data[1] = special_report->data[1];
0165 roccat_report_event(savu->chrdev_minor,
0166 (uint8_t const *)&roccat_report);
0167 }
0168
0169 static int savu_raw_event(struct hid_device *hdev,
0170 struct hid_report *report, u8 *data, int size)
0171 {
0172 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0173 struct roccat_common2_device *savu = hid_get_drvdata(hdev);
0174
0175 if (intf->cur_altsetting->desc.bInterfaceProtocol
0176 != USB_INTERFACE_PROTOCOL_MOUSE)
0177 return 0;
0178
0179 if (savu == NULL)
0180 return 0;
0181
0182 if (savu->roccat_claimed)
0183 savu_report_to_chrdev(savu, data);
0184
0185 return 0;
0186 }
0187
0188 static const struct hid_device_id savu_devices[] = {
0189 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
0190 { }
0191 };
0192
0193 MODULE_DEVICE_TABLE(hid, savu_devices);
0194
0195 static struct hid_driver savu_driver = {
0196 .name = "savu",
0197 .id_table = savu_devices,
0198 .probe = savu_probe,
0199 .remove = savu_remove,
0200 .raw_event = savu_raw_event
0201 };
0202
0203 static int __init savu_init(void)
0204 {
0205 int retval;
0206
0207 savu_class = class_create(THIS_MODULE, "savu");
0208 if (IS_ERR(savu_class))
0209 return PTR_ERR(savu_class);
0210 savu_class->dev_groups = savu_groups;
0211
0212 retval = hid_register_driver(&savu_driver);
0213 if (retval)
0214 class_destroy(savu_class);
0215 return retval;
0216 }
0217
0218 static void __exit savu_exit(void)
0219 {
0220 hid_unregister_driver(&savu_driver);
0221 class_destroy(savu_class);
0222 }
0223
0224 module_init(savu_init);
0225 module_exit(savu_exit);
0226
0227 MODULE_AUTHOR("Stefan Achatz");
0228 MODULE_DESCRIPTION("USB Roccat Savu driver");
0229 MODULE_LICENSE("GPL v2");