12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
26a2a6390SStefan Achatz /*
36a2a6390SStefan Achatz * Roccat Savu driver for Linux
46a2a6390SStefan Achatz *
56a2a6390SStefan Achatz * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net>
66a2a6390SStefan Achatz */
76a2a6390SStefan Achatz
86a2a6390SStefan Achatz /*
96a2a6390SStefan Achatz */
106a2a6390SStefan Achatz
116a2a6390SStefan Achatz /* Roccat Savu is a gamer mouse with macro keys that can be configured in
126a2a6390SStefan Achatz * 5 profiles.
136a2a6390SStefan Achatz */
146a2a6390SStefan Achatz
156a2a6390SStefan Achatz #include <linux/device.h>
166a2a6390SStefan Achatz #include <linux/input.h>
176a2a6390SStefan Achatz #include <linux/hid.h>
186a2a6390SStefan Achatz #include <linux/module.h>
196a2a6390SStefan Achatz #include <linux/slab.h>
206a2a6390SStefan Achatz #include <linux/hid-roccat.h>
216a2a6390SStefan Achatz #include "hid-ids.h"
226a2a6390SStefan Achatz #include "hid-roccat-common.h"
236a2a6390SStefan Achatz #include "hid-roccat-savu.h"
246a2a6390SStefan Achatz
2571304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x4, 0x03);
2671304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x5, 0x03);
2771304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(general, 0x6, 0x10);
2871304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(buttons, 0x7, 0x2f);
2971304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x8, 0x0823);
3071304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x9, 0x08);
3171304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0xc, 0x04);
326a2a6390SStefan Achatz
3371304f5aSStefan Achatz static struct bin_attribute *savu_bin_attrs[] = {
3408079548SGreg Kroah-Hartman &bin_attr_control,
3508079548SGreg Kroah-Hartman &bin_attr_profile,
3608079548SGreg Kroah-Hartman &bin_attr_general,
3708079548SGreg Kroah-Hartman &bin_attr_buttons,
3808079548SGreg Kroah-Hartman &bin_attr_macro,
3908079548SGreg Kroah-Hartman &bin_attr_info,
4008079548SGreg Kroah-Hartman &bin_attr_sensor,
4108079548SGreg Kroah-Hartman NULL,
4208079548SGreg Kroah-Hartman };
4308079548SGreg Kroah-Hartman
4408079548SGreg Kroah-Hartman static const struct attribute_group savu_group = {
4571304f5aSStefan Achatz .bin_attrs = savu_bin_attrs,
4608079548SGreg Kroah-Hartman };
4708079548SGreg Kroah-Hartman
4808079548SGreg Kroah-Hartman static const struct attribute_group *savu_groups[] = {
4908079548SGreg Kroah-Hartman &savu_group,
5008079548SGreg Kroah-Hartman NULL,
516a2a6390SStefan Achatz };
526a2a6390SStefan Achatz
53*afdf5dd3SIvan Orlov static const struct class savu_class = {
54*afdf5dd3SIvan Orlov .name = "savu",
55*afdf5dd3SIvan Orlov .dev_groups = savu_groups,
56*afdf5dd3SIvan Orlov };
57*afdf5dd3SIvan Orlov
savu_init_specials(struct hid_device * hdev)586a2a6390SStefan Achatz static int savu_init_specials(struct hid_device *hdev)
596a2a6390SStefan Achatz {
606a2a6390SStefan Achatz struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
616a2a6390SStefan Achatz struct usb_device *usb_dev = interface_to_usbdev(intf);
6271304f5aSStefan Achatz struct roccat_common2_device *savu;
636a2a6390SStefan Achatz int retval;
646a2a6390SStefan Achatz
656a2a6390SStefan Achatz if (intf->cur_altsetting->desc.bInterfaceProtocol
666a2a6390SStefan Achatz != USB_INTERFACE_PROTOCOL_MOUSE) {
676a2a6390SStefan Achatz hid_set_drvdata(hdev, NULL);
686a2a6390SStefan Achatz return 0;
696a2a6390SStefan Achatz }
706a2a6390SStefan Achatz
716a2a6390SStefan Achatz savu = kzalloc(sizeof(*savu), GFP_KERNEL);
726a2a6390SStefan Achatz if (!savu) {
736a2a6390SStefan Achatz hid_err(hdev, "can't alloc device descriptor\n");
746a2a6390SStefan Achatz return -ENOMEM;
756a2a6390SStefan Achatz }
766a2a6390SStefan Achatz hid_set_drvdata(hdev, savu);
776a2a6390SStefan Achatz
7871304f5aSStefan Achatz retval = roccat_common2_device_init_struct(usb_dev, savu);
796a2a6390SStefan Achatz if (retval) {
8071304f5aSStefan Achatz hid_err(hdev, "couldn't init Savu device\n");
816a2a6390SStefan Achatz goto exit_free;
826a2a6390SStefan Achatz }
836a2a6390SStefan Achatz
84*afdf5dd3SIvan Orlov retval = roccat_connect(&savu_class, hdev,
856a2a6390SStefan Achatz sizeof(struct savu_roccat_report));
866a2a6390SStefan Achatz if (retval < 0) {
876a2a6390SStefan Achatz hid_err(hdev, "couldn't init char dev\n");
886a2a6390SStefan Achatz } else {
896a2a6390SStefan Achatz savu->chrdev_minor = retval;
906a2a6390SStefan Achatz savu->roccat_claimed = 1;
916a2a6390SStefan Achatz }
926a2a6390SStefan Achatz
936a2a6390SStefan Achatz return 0;
946a2a6390SStefan Achatz exit_free:
956a2a6390SStefan Achatz kfree(savu);
966a2a6390SStefan Achatz return retval;
976a2a6390SStefan Achatz }
986a2a6390SStefan Achatz
savu_remove_specials(struct hid_device * hdev)996a2a6390SStefan Achatz static void savu_remove_specials(struct hid_device *hdev)
1006a2a6390SStefan Achatz {
1016a2a6390SStefan Achatz struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
10271304f5aSStefan Achatz struct roccat_common2_device *savu;
1036a2a6390SStefan Achatz
1046a2a6390SStefan Achatz if (intf->cur_altsetting->desc.bInterfaceProtocol
1056a2a6390SStefan Achatz != USB_INTERFACE_PROTOCOL_MOUSE)
1066a2a6390SStefan Achatz return;
1076a2a6390SStefan Achatz
1086a2a6390SStefan Achatz savu = hid_get_drvdata(hdev);
1096a2a6390SStefan Achatz if (savu->roccat_claimed)
1106a2a6390SStefan Achatz roccat_disconnect(savu->chrdev_minor);
1116a2a6390SStefan Achatz kfree(savu);
1126a2a6390SStefan Achatz }
1136a2a6390SStefan Achatz
savu_probe(struct hid_device * hdev,const struct hid_device_id * id)1146a2a6390SStefan Achatz static int savu_probe(struct hid_device *hdev,
1156a2a6390SStefan Achatz const struct hid_device_id *id)
1166a2a6390SStefan Achatz {
1176a2a6390SStefan Achatz int retval;
1186a2a6390SStefan Achatz
11993020953SGreg Kroah-Hartman if (!hid_is_usb(hdev))
12093020953SGreg Kroah-Hartman return -EINVAL;
12193020953SGreg Kroah-Hartman
1226a2a6390SStefan Achatz retval = hid_parse(hdev);
1236a2a6390SStefan Achatz if (retval) {
1246a2a6390SStefan Achatz hid_err(hdev, "parse failed\n");
1256a2a6390SStefan Achatz goto exit;
1266a2a6390SStefan Achatz }
1276a2a6390SStefan Achatz
1286a2a6390SStefan Achatz retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1296a2a6390SStefan Achatz if (retval) {
1306a2a6390SStefan Achatz hid_err(hdev, "hw start failed\n");
1316a2a6390SStefan Achatz goto exit;
1326a2a6390SStefan Achatz }
1336a2a6390SStefan Achatz
1346a2a6390SStefan Achatz retval = savu_init_specials(hdev);
1356a2a6390SStefan Achatz if (retval) {
1366a2a6390SStefan Achatz hid_err(hdev, "couldn't install mouse\n");
1376a2a6390SStefan Achatz goto exit_stop;
1386a2a6390SStefan Achatz }
1396a2a6390SStefan Achatz
1406a2a6390SStefan Achatz return 0;
1416a2a6390SStefan Achatz
1426a2a6390SStefan Achatz exit_stop:
1436a2a6390SStefan Achatz hid_hw_stop(hdev);
1446a2a6390SStefan Achatz exit:
1456a2a6390SStefan Achatz return retval;
1466a2a6390SStefan Achatz }
1476a2a6390SStefan Achatz
savu_remove(struct hid_device * hdev)1486a2a6390SStefan Achatz static void savu_remove(struct hid_device *hdev)
1496a2a6390SStefan Achatz {
1506a2a6390SStefan Achatz savu_remove_specials(hdev);
1516a2a6390SStefan Achatz hid_hw_stop(hdev);
1526a2a6390SStefan Achatz }
1536a2a6390SStefan Achatz
savu_report_to_chrdev(struct roccat_common2_device const * savu,u8 const * data)15471304f5aSStefan Achatz static void savu_report_to_chrdev(struct roccat_common2_device const *savu,
1556a2a6390SStefan Achatz u8 const *data)
1566a2a6390SStefan Achatz {
1576a2a6390SStefan Achatz struct savu_roccat_report roccat_report;
1586a2a6390SStefan Achatz struct savu_mouse_report_special const *special_report;
1596a2a6390SStefan Achatz
1606a2a6390SStefan Achatz if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL)
1616a2a6390SStefan Achatz return;
1626a2a6390SStefan Achatz
1636a2a6390SStefan Achatz special_report = (struct savu_mouse_report_special const *)data;
1646a2a6390SStefan Achatz
1656a2a6390SStefan Achatz roccat_report.type = special_report->type;
1666a2a6390SStefan Achatz roccat_report.data[0] = special_report->data[0];
1676a2a6390SStefan Achatz roccat_report.data[1] = special_report->data[1];
1686a2a6390SStefan Achatz roccat_report_event(savu->chrdev_minor,
1696a2a6390SStefan Achatz (uint8_t const *)&roccat_report);
1706a2a6390SStefan Achatz }
1716a2a6390SStefan Achatz
savu_raw_event(struct hid_device * hdev,struct hid_report * report,u8 * data,int size)1726a2a6390SStefan Achatz static int savu_raw_event(struct hid_device *hdev,
1736a2a6390SStefan Achatz struct hid_report *report, u8 *data, int size)
1746a2a6390SStefan Achatz {
1756a2a6390SStefan Achatz struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
17671304f5aSStefan Achatz struct roccat_common2_device *savu = hid_get_drvdata(hdev);
1776a2a6390SStefan Achatz
1786a2a6390SStefan Achatz if (intf->cur_altsetting->desc.bInterfaceProtocol
1796a2a6390SStefan Achatz != USB_INTERFACE_PROTOCOL_MOUSE)
1806a2a6390SStefan Achatz return 0;
1816a2a6390SStefan Achatz
1826a2a6390SStefan Achatz if (savu == NULL)
1836a2a6390SStefan Achatz return 0;
1846a2a6390SStefan Achatz
1856a2a6390SStefan Achatz if (savu->roccat_claimed)
1866a2a6390SStefan Achatz savu_report_to_chrdev(savu, data);
1876a2a6390SStefan Achatz
1886a2a6390SStefan Achatz return 0;
1896a2a6390SStefan Achatz }
1906a2a6390SStefan Achatz
1916a2a6390SStefan Achatz static const struct hid_device_id savu_devices[] = {
1926a2a6390SStefan Achatz { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
1936a2a6390SStefan Achatz { }
1946a2a6390SStefan Achatz };
1956a2a6390SStefan Achatz
1966a2a6390SStefan Achatz MODULE_DEVICE_TABLE(hid, savu_devices);
1976a2a6390SStefan Achatz
1986a2a6390SStefan Achatz static struct hid_driver savu_driver = {
1996a2a6390SStefan Achatz .name = "savu",
2006a2a6390SStefan Achatz .id_table = savu_devices,
2016a2a6390SStefan Achatz .probe = savu_probe,
2026a2a6390SStefan Achatz .remove = savu_remove,
2036a2a6390SStefan Achatz .raw_event = savu_raw_event
2046a2a6390SStefan Achatz };
2056a2a6390SStefan Achatz
savu_init(void)2066a2a6390SStefan Achatz static int __init savu_init(void)
2076a2a6390SStefan Achatz {
2086a2a6390SStefan Achatz int retval;
2096a2a6390SStefan Achatz
210*afdf5dd3SIvan Orlov retval = class_register(&savu_class);
211*afdf5dd3SIvan Orlov if (retval)
212*afdf5dd3SIvan Orlov return retval;
2136a2a6390SStefan Achatz
2146a2a6390SStefan Achatz retval = hid_register_driver(&savu_driver);
2156a2a6390SStefan Achatz if (retval)
216*afdf5dd3SIvan Orlov class_unregister(&savu_class);
2176a2a6390SStefan Achatz return retval;
2186a2a6390SStefan Achatz }
2196a2a6390SStefan Achatz
savu_exit(void)2206a2a6390SStefan Achatz static void __exit savu_exit(void)
2216a2a6390SStefan Achatz {
2226a2a6390SStefan Achatz hid_unregister_driver(&savu_driver);
223*afdf5dd3SIvan Orlov class_unregister(&savu_class);
2246a2a6390SStefan Achatz }
2256a2a6390SStefan Achatz
2266a2a6390SStefan Achatz module_init(savu_init);
2276a2a6390SStefan Achatz module_exit(savu_exit);
2286a2a6390SStefan Achatz
2296a2a6390SStefan Achatz MODULE_AUTHOR("Stefan Achatz");
2306a2a6390SStefan Achatz MODULE_DESCRIPTION("USB Roccat Savu driver");
2316a2a6390SStefan Achatz MODULE_LICENSE("GPL v2");
232