xref: /linux/drivers/hid/hid-roccat-savu.c (revision 2874c5fd284268364ece81a7bd936f3c8168e567)
1*2874c5fdSThomas 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 
256a2a6390SStefan Achatz static struct class *savu_class;
266a2a6390SStefan Achatz 
2771304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x4, 0x03);
2871304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x5, 0x03);
2971304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(general, 0x6, 0x10);
3071304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(buttons, 0x7, 0x2f);
3171304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x8, 0x0823);
3271304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x9, 0x08);
3371304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0xc, 0x04);
346a2a6390SStefan Achatz 
3571304f5aSStefan Achatz static struct bin_attribute *savu_bin_attrs[] = {
3608079548SGreg Kroah-Hartman 	&bin_attr_control,
3708079548SGreg Kroah-Hartman 	&bin_attr_profile,
3808079548SGreg Kroah-Hartman 	&bin_attr_general,
3908079548SGreg Kroah-Hartman 	&bin_attr_buttons,
4008079548SGreg Kroah-Hartman 	&bin_attr_macro,
4108079548SGreg Kroah-Hartman 	&bin_attr_info,
4208079548SGreg Kroah-Hartman 	&bin_attr_sensor,
4308079548SGreg Kroah-Hartman 	NULL,
4408079548SGreg Kroah-Hartman };
4508079548SGreg Kroah-Hartman 
4608079548SGreg Kroah-Hartman static const struct attribute_group savu_group = {
4771304f5aSStefan Achatz 	.bin_attrs = savu_bin_attrs,
4808079548SGreg Kroah-Hartman };
4908079548SGreg Kroah-Hartman 
5008079548SGreg Kroah-Hartman static const struct attribute_group *savu_groups[] = {
5108079548SGreg Kroah-Hartman 	&savu_group,
5208079548SGreg Kroah-Hartman 	NULL,
536a2a6390SStefan Achatz };
546a2a6390SStefan Achatz 
556a2a6390SStefan Achatz static int savu_init_specials(struct hid_device *hdev)
566a2a6390SStefan Achatz {
576a2a6390SStefan Achatz 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
586a2a6390SStefan Achatz 	struct usb_device *usb_dev = interface_to_usbdev(intf);
5971304f5aSStefan Achatz 	struct roccat_common2_device *savu;
606a2a6390SStefan Achatz 	int retval;
616a2a6390SStefan Achatz 
626a2a6390SStefan Achatz 	if (intf->cur_altsetting->desc.bInterfaceProtocol
636a2a6390SStefan Achatz 			!= USB_INTERFACE_PROTOCOL_MOUSE) {
646a2a6390SStefan Achatz 		hid_set_drvdata(hdev, NULL);
656a2a6390SStefan Achatz 		return 0;
666a2a6390SStefan Achatz 	}
676a2a6390SStefan Achatz 
686a2a6390SStefan Achatz 	savu = kzalloc(sizeof(*savu), GFP_KERNEL);
696a2a6390SStefan Achatz 	if (!savu) {
706a2a6390SStefan Achatz 		hid_err(hdev, "can't alloc device descriptor\n");
716a2a6390SStefan Achatz 		return -ENOMEM;
726a2a6390SStefan Achatz 	}
736a2a6390SStefan Achatz 	hid_set_drvdata(hdev, savu);
746a2a6390SStefan Achatz 
7571304f5aSStefan Achatz 	retval = roccat_common2_device_init_struct(usb_dev, savu);
766a2a6390SStefan Achatz 	if (retval) {
7771304f5aSStefan Achatz 		hid_err(hdev, "couldn't init Savu device\n");
786a2a6390SStefan Achatz 		goto exit_free;
796a2a6390SStefan Achatz 	}
806a2a6390SStefan Achatz 
816a2a6390SStefan Achatz 	retval = roccat_connect(savu_class, hdev,
826a2a6390SStefan Achatz 			sizeof(struct savu_roccat_report));
836a2a6390SStefan Achatz 	if (retval < 0) {
846a2a6390SStefan Achatz 		hid_err(hdev, "couldn't init char dev\n");
856a2a6390SStefan Achatz 	} else {
866a2a6390SStefan Achatz 		savu->chrdev_minor = retval;
876a2a6390SStefan Achatz 		savu->roccat_claimed = 1;
886a2a6390SStefan Achatz 	}
896a2a6390SStefan Achatz 
906a2a6390SStefan Achatz 	return 0;
916a2a6390SStefan Achatz exit_free:
926a2a6390SStefan Achatz 	kfree(savu);
936a2a6390SStefan Achatz 	return retval;
946a2a6390SStefan Achatz }
956a2a6390SStefan Achatz 
966a2a6390SStefan Achatz static void savu_remove_specials(struct hid_device *hdev)
976a2a6390SStefan Achatz {
986a2a6390SStefan Achatz 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
9971304f5aSStefan Achatz 	struct roccat_common2_device *savu;
1006a2a6390SStefan Achatz 
1016a2a6390SStefan Achatz 	if (intf->cur_altsetting->desc.bInterfaceProtocol
1026a2a6390SStefan Achatz 			!= USB_INTERFACE_PROTOCOL_MOUSE)
1036a2a6390SStefan Achatz 		return;
1046a2a6390SStefan Achatz 
1056a2a6390SStefan Achatz 	savu = hid_get_drvdata(hdev);
1066a2a6390SStefan Achatz 	if (savu->roccat_claimed)
1076a2a6390SStefan Achatz 		roccat_disconnect(savu->chrdev_minor);
1086a2a6390SStefan Achatz 	kfree(savu);
1096a2a6390SStefan Achatz }
1106a2a6390SStefan Achatz 
1116a2a6390SStefan Achatz static int savu_probe(struct hid_device *hdev,
1126a2a6390SStefan Achatz 		const struct hid_device_id *id)
1136a2a6390SStefan Achatz {
1146a2a6390SStefan Achatz 	int retval;
1156a2a6390SStefan Achatz 
1166a2a6390SStefan Achatz 	retval = hid_parse(hdev);
1176a2a6390SStefan Achatz 	if (retval) {
1186a2a6390SStefan Achatz 		hid_err(hdev, "parse failed\n");
1196a2a6390SStefan Achatz 		goto exit;
1206a2a6390SStefan Achatz 	}
1216a2a6390SStefan Achatz 
1226a2a6390SStefan Achatz 	retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
1236a2a6390SStefan Achatz 	if (retval) {
1246a2a6390SStefan Achatz 		hid_err(hdev, "hw start failed\n");
1256a2a6390SStefan Achatz 		goto exit;
1266a2a6390SStefan Achatz 	}
1276a2a6390SStefan Achatz 
1286a2a6390SStefan Achatz 	retval = savu_init_specials(hdev);
1296a2a6390SStefan Achatz 	if (retval) {
1306a2a6390SStefan Achatz 		hid_err(hdev, "couldn't install mouse\n");
1316a2a6390SStefan Achatz 		goto exit_stop;
1326a2a6390SStefan Achatz 	}
1336a2a6390SStefan Achatz 
1346a2a6390SStefan Achatz 	return 0;
1356a2a6390SStefan Achatz 
1366a2a6390SStefan Achatz exit_stop:
1376a2a6390SStefan Achatz 	hid_hw_stop(hdev);
1386a2a6390SStefan Achatz exit:
1396a2a6390SStefan Achatz 	return retval;
1406a2a6390SStefan Achatz }
1416a2a6390SStefan Achatz 
1426a2a6390SStefan Achatz static void savu_remove(struct hid_device *hdev)
1436a2a6390SStefan Achatz {
1446a2a6390SStefan Achatz 	savu_remove_specials(hdev);
1456a2a6390SStefan Achatz 	hid_hw_stop(hdev);
1466a2a6390SStefan Achatz }
1476a2a6390SStefan Achatz 
14871304f5aSStefan Achatz static void savu_report_to_chrdev(struct roccat_common2_device const *savu,
1496a2a6390SStefan Achatz 		u8 const *data)
1506a2a6390SStefan Achatz {
1516a2a6390SStefan Achatz 	struct savu_roccat_report roccat_report;
1526a2a6390SStefan Achatz 	struct savu_mouse_report_special const *special_report;
1536a2a6390SStefan Achatz 
1546a2a6390SStefan Achatz 	if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL)
1556a2a6390SStefan Achatz 		return;
1566a2a6390SStefan Achatz 
1576a2a6390SStefan Achatz 	special_report = (struct savu_mouse_report_special const *)data;
1586a2a6390SStefan Achatz 
1596a2a6390SStefan Achatz 	roccat_report.type = special_report->type;
1606a2a6390SStefan Achatz 	roccat_report.data[0] = special_report->data[0];
1616a2a6390SStefan Achatz 	roccat_report.data[1] = special_report->data[1];
1626a2a6390SStefan Achatz 	roccat_report_event(savu->chrdev_minor,
1636a2a6390SStefan Achatz 			(uint8_t const *)&roccat_report);
1646a2a6390SStefan Achatz }
1656a2a6390SStefan Achatz 
1666a2a6390SStefan Achatz static int savu_raw_event(struct hid_device *hdev,
1676a2a6390SStefan Achatz 		struct hid_report *report, u8 *data, int size)
1686a2a6390SStefan Achatz {
1696a2a6390SStefan Achatz 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
17071304f5aSStefan Achatz 	struct roccat_common2_device *savu = hid_get_drvdata(hdev);
1716a2a6390SStefan Achatz 
1726a2a6390SStefan Achatz 	if (intf->cur_altsetting->desc.bInterfaceProtocol
1736a2a6390SStefan Achatz 			!= USB_INTERFACE_PROTOCOL_MOUSE)
1746a2a6390SStefan Achatz 		return 0;
1756a2a6390SStefan Achatz 
1766a2a6390SStefan Achatz 	if (savu == NULL)
1776a2a6390SStefan Achatz 		return 0;
1786a2a6390SStefan Achatz 
1796a2a6390SStefan Achatz 	if (savu->roccat_claimed)
1806a2a6390SStefan Achatz 		savu_report_to_chrdev(savu, data);
1816a2a6390SStefan Achatz 
1826a2a6390SStefan Achatz 	return 0;
1836a2a6390SStefan Achatz }
1846a2a6390SStefan Achatz 
1856a2a6390SStefan Achatz static const struct hid_device_id savu_devices[] = {
1866a2a6390SStefan Achatz 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
1876a2a6390SStefan Achatz 	{ }
1886a2a6390SStefan Achatz };
1896a2a6390SStefan Achatz 
1906a2a6390SStefan Achatz MODULE_DEVICE_TABLE(hid, savu_devices);
1916a2a6390SStefan Achatz 
1926a2a6390SStefan Achatz static struct hid_driver savu_driver = {
1936a2a6390SStefan Achatz 		.name = "savu",
1946a2a6390SStefan Achatz 		.id_table = savu_devices,
1956a2a6390SStefan Achatz 		.probe = savu_probe,
1966a2a6390SStefan Achatz 		.remove = savu_remove,
1976a2a6390SStefan Achatz 		.raw_event = savu_raw_event
1986a2a6390SStefan Achatz };
1996a2a6390SStefan Achatz 
2006a2a6390SStefan Achatz static int __init savu_init(void)
2016a2a6390SStefan Achatz {
2026a2a6390SStefan Achatz 	int retval;
2036a2a6390SStefan Achatz 
2046a2a6390SStefan Achatz 	savu_class = class_create(THIS_MODULE, "savu");
2056a2a6390SStefan Achatz 	if (IS_ERR(savu_class))
2066a2a6390SStefan Achatz 		return PTR_ERR(savu_class);
20708079548SGreg Kroah-Hartman 	savu_class->dev_groups = savu_groups;
2086a2a6390SStefan Achatz 
2096a2a6390SStefan Achatz 	retval = hid_register_driver(&savu_driver);
2106a2a6390SStefan Achatz 	if (retval)
2116a2a6390SStefan Achatz 		class_destroy(savu_class);
2126a2a6390SStefan Achatz 	return retval;
2136a2a6390SStefan Achatz }
2146a2a6390SStefan Achatz 
2156a2a6390SStefan Achatz static void __exit savu_exit(void)
2166a2a6390SStefan Achatz {
2176a2a6390SStefan Achatz 	hid_unregister_driver(&savu_driver);
2186a2a6390SStefan Achatz 	class_destroy(savu_class);
2196a2a6390SStefan Achatz }
2206a2a6390SStefan Achatz 
2216a2a6390SStefan Achatz module_init(savu_init);
2226a2a6390SStefan Achatz module_exit(savu_exit);
2236a2a6390SStefan Achatz 
2246a2a6390SStefan Achatz MODULE_AUTHOR("Stefan Achatz");
2256a2a6390SStefan Achatz MODULE_DESCRIPTION("USB Roccat Savu driver");
2266a2a6390SStefan Achatz MODULE_LICENSE("GPL v2");
227