16a2a6390SStefan Achatz /* 26a2a6390SStefan Achatz * Roccat Savu driver for Linux 36a2a6390SStefan Achatz * 46a2a6390SStefan Achatz * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net> 56a2a6390SStefan Achatz */ 66a2a6390SStefan Achatz 76a2a6390SStefan Achatz /* 86a2a6390SStefan Achatz * This program is free software; you can redistribute it and/or modify it 96a2a6390SStefan Achatz * under the terms of the GNU General Public License as published by the Free 106a2a6390SStefan Achatz * Software Foundation; either version 2 of the License, or (at your option) 116a2a6390SStefan Achatz * any later version. 126a2a6390SStefan Achatz */ 136a2a6390SStefan Achatz 146a2a6390SStefan Achatz /* Roccat Savu is a gamer mouse with macro keys that can be configured in 156a2a6390SStefan Achatz * 5 profiles. 166a2a6390SStefan Achatz */ 176a2a6390SStefan Achatz 186a2a6390SStefan Achatz #include <linux/device.h> 196a2a6390SStefan Achatz #include <linux/input.h> 206a2a6390SStefan Achatz #include <linux/hid.h> 216a2a6390SStefan Achatz #include <linux/module.h> 226a2a6390SStefan Achatz #include <linux/slab.h> 236a2a6390SStefan Achatz #include <linux/hid-roccat.h> 246a2a6390SStefan Achatz #include "hid-ids.h" 256a2a6390SStefan Achatz #include "hid-roccat-common.h" 266a2a6390SStefan Achatz #include "hid-roccat-savu.h" 276a2a6390SStefan Achatz 286a2a6390SStefan Achatz static struct class *savu_class; 296a2a6390SStefan Achatz 30*71304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_W(control, 0x4, 0x03); 31*71304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(profile, 0x5, 0x03); 32*71304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(general, 0x6, 0x10); 33*71304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(buttons, 0x7, 0x2f); 34*71304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(macro, 0x8, 0x0823); 35*71304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(info, 0x9, 0x08); 36*71304f5aSStefan Achatz ROCCAT_COMMON2_BIN_ATTRIBUTE_RW(sensor, 0xc, 0x04); 376a2a6390SStefan Achatz 38*71304f5aSStefan Achatz static struct bin_attribute *savu_bin_attrs[] = { 3908079548SGreg Kroah-Hartman &bin_attr_control, 4008079548SGreg Kroah-Hartman &bin_attr_profile, 4108079548SGreg Kroah-Hartman &bin_attr_general, 4208079548SGreg Kroah-Hartman &bin_attr_buttons, 4308079548SGreg Kroah-Hartman &bin_attr_macro, 4408079548SGreg Kroah-Hartman &bin_attr_info, 4508079548SGreg Kroah-Hartman &bin_attr_sensor, 4608079548SGreg Kroah-Hartman NULL, 4708079548SGreg Kroah-Hartman }; 4808079548SGreg Kroah-Hartman 4908079548SGreg Kroah-Hartman static const struct attribute_group savu_group = { 50*71304f5aSStefan Achatz .bin_attrs = savu_bin_attrs, 5108079548SGreg Kroah-Hartman }; 5208079548SGreg Kroah-Hartman 5308079548SGreg Kroah-Hartman static const struct attribute_group *savu_groups[] = { 5408079548SGreg Kroah-Hartman &savu_group, 5508079548SGreg Kroah-Hartman NULL, 566a2a6390SStefan Achatz }; 576a2a6390SStefan Achatz 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); 62*71304f5aSStefan 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 78*71304f5aSStefan Achatz retval = roccat_common2_device_init_struct(usb_dev, savu); 796a2a6390SStefan Achatz if (retval) { 80*71304f5aSStefan Achatz hid_err(hdev, "couldn't init Savu device\n"); 816a2a6390SStefan Achatz goto exit_free; 826a2a6390SStefan Achatz } 836a2a6390SStefan Achatz 846a2a6390SStefan Achatz 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 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); 102*71304f5aSStefan 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 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 1196a2a6390SStefan Achatz retval = hid_parse(hdev); 1206a2a6390SStefan Achatz if (retval) { 1216a2a6390SStefan Achatz hid_err(hdev, "parse failed\n"); 1226a2a6390SStefan Achatz goto exit; 1236a2a6390SStefan Achatz } 1246a2a6390SStefan Achatz 1256a2a6390SStefan Achatz retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT); 1266a2a6390SStefan Achatz if (retval) { 1276a2a6390SStefan Achatz hid_err(hdev, "hw start failed\n"); 1286a2a6390SStefan Achatz goto exit; 1296a2a6390SStefan Achatz } 1306a2a6390SStefan Achatz 1316a2a6390SStefan Achatz retval = savu_init_specials(hdev); 1326a2a6390SStefan Achatz if (retval) { 1336a2a6390SStefan Achatz hid_err(hdev, "couldn't install mouse\n"); 1346a2a6390SStefan Achatz goto exit_stop; 1356a2a6390SStefan Achatz } 1366a2a6390SStefan Achatz 1376a2a6390SStefan Achatz return 0; 1386a2a6390SStefan Achatz 1396a2a6390SStefan Achatz exit_stop: 1406a2a6390SStefan Achatz hid_hw_stop(hdev); 1416a2a6390SStefan Achatz exit: 1426a2a6390SStefan Achatz return retval; 1436a2a6390SStefan Achatz } 1446a2a6390SStefan Achatz 1456a2a6390SStefan Achatz static void savu_remove(struct hid_device *hdev) 1466a2a6390SStefan Achatz { 1476a2a6390SStefan Achatz savu_remove_specials(hdev); 1486a2a6390SStefan Achatz hid_hw_stop(hdev); 1496a2a6390SStefan Achatz } 1506a2a6390SStefan Achatz 151*71304f5aSStefan Achatz static void savu_report_to_chrdev(struct roccat_common2_device const *savu, 1526a2a6390SStefan Achatz u8 const *data) 1536a2a6390SStefan Achatz { 1546a2a6390SStefan Achatz struct savu_roccat_report roccat_report; 1556a2a6390SStefan Achatz struct savu_mouse_report_special const *special_report; 1566a2a6390SStefan Achatz 1576a2a6390SStefan Achatz if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL) 1586a2a6390SStefan Achatz return; 1596a2a6390SStefan Achatz 1606a2a6390SStefan Achatz special_report = (struct savu_mouse_report_special const *)data; 1616a2a6390SStefan Achatz 1626a2a6390SStefan Achatz roccat_report.type = special_report->type; 1636a2a6390SStefan Achatz roccat_report.data[0] = special_report->data[0]; 1646a2a6390SStefan Achatz roccat_report.data[1] = special_report->data[1]; 1656a2a6390SStefan Achatz roccat_report_event(savu->chrdev_minor, 1666a2a6390SStefan Achatz (uint8_t const *)&roccat_report); 1676a2a6390SStefan Achatz } 1686a2a6390SStefan Achatz 1696a2a6390SStefan Achatz static int savu_raw_event(struct hid_device *hdev, 1706a2a6390SStefan Achatz struct hid_report *report, u8 *data, int size) 1716a2a6390SStefan Achatz { 1726a2a6390SStefan Achatz struct usb_interface *intf = to_usb_interface(hdev->dev.parent); 173*71304f5aSStefan Achatz struct roccat_common2_device *savu = hid_get_drvdata(hdev); 1746a2a6390SStefan Achatz 1756a2a6390SStefan Achatz if (intf->cur_altsetting->desc.bInterfaceProtocol 1766a2a6390SStefan Achatz != USB_INTERFACE_PROTOCOL_MOUSE) 1776a2a6390SStefan Achatz return 0; 1786a2a6390SStefan Achatz 1796a2a6390SStefan Achatz if (savu == NULL) 1806a2a6390SStefan Achatz return 0; 1816a2a6390SStefan Achatz 1826a2a6390SStefan Achatz if (savu->roccat_claimed) 1836a2a6390SStefan Achatz savu_report_to_chrdev(savu, data); 1846a2a6390SStefan Achatz 1856a2a6390SStefan Achatz return 0; 1866a2a6390SStefan Achatz } 1876a2a6390SStefan Achatz 1886a2a6390SStefan Achatz static const struct hid_device_id savu_devices[] = { 1896a2a6390SStefan Achatz { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) }, 1906a2a6390SStefan Achatz { } 1916a2a6390SStefan Achatz }; 1926a2a6390SStefan Achatz 1936a2a6390SStefan Achatz MODULE_DEVICE_TABLE(hid, savu_devices); 1946a2a6390SStefan Achatz 1956a2a6390SStefan Achatz static struct hid_driver savu_driver = { 1966a2a6390SStefan Achatz .name = "savu", 1976a2a6390SStefan Achatz .id_table = savu_devices, 1986a2a6390SStefan Achatz .probe = savu_probe, 1996a2a6390SStefan Achatz .remove = savu_remove, 2006a2a6390SStefan Achatz .raw_event = savu_raw_event 2016a2a6390SStefan Achatz }; 2026a2a6390SStefan Achatz 2036a2a6390SStefan Achatz static int __init savu_init(void) 2046a2a6390SStefan Achatz { 2056a2a6390SStefan Achatz int retval; 2066a2a6390SStefan Achatz 2076a2a6390SStefan Achatz savu_class = class_create(THIS_MODULE, "savu"); 2086a2a6390SStefan Achatz if (IS_ERR(savu_class)) 2096a2a6390SStefan Achatz return PTR_ERR(savu_class); 21008079548SGreg Kroah-Hartman savu_class->dev_groups = savu_groups; 2116a2a6390SStefan Achatz 2126a2a6390SStefan Achatz retval = hid_register_driver(&savu_driver); 2136a2a6390SStefan Achatz if (retval) 2146a2a6390SStefan Achatz class_destroy(savu_class); 2156a2a6390SStefan Achatz return retval; 2166a2a6390SStefan Achatz } 2176a2a6390SStefan Achatz 2186a2a6390SStefan Achatz static void __exit savu_exit(void) 2196a2a6390SStefan Achatz { 2206a2a6390SStefan Achatz hid_unregister_driver(&savu_driver); 2216a2a6390SStefan Achatz class_destroy(savu_class); 2226a2a6390SStefan Achatz } 2236a2a6390SStefan Achatz 2246a2a6390SStefan Achatz module_init(savu_init); 2256a2a6390SStefan Achatz module_exit(savu_exit); 2266a2a6390SStefan Achatz 2276a2a6390SStefan Achatz MODULE_AUTHOR("Stefan Achatz"); 2286a2a6390SStefan Achatz MODULE_DESCRIPTION("USB Roccat Savu driver"); 2296a2a6390SStefan Achatz MODULE_LICENSE("GPL v2"); 230