Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Roccat Lua driver for Linux
0004  *
0005  * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net>
0006  */
0007 
0008 /*
0009  */
0010 
0011 /*
0012  * Roccat Lua is a gamer mouse which cpi, button and light settings can be
0013  * configured.
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-lua.h"
0025 
0026 static ssize_t lua_sysfs_read(struct file *fp, struct kobject *kobj,
0027         char *buf, loff_t off, size_t count,
0028         size_t real_size, uint command)
0029 {
0030     struct device *dev = kobj_to_dev(kobj);
0031     struct lua_device *lua = hid_get_drvdata(dev_get_drvdata(dev));
0032     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0033     int retval;
0034 
0035     if (off >= real_size)
0036         return 0;
0037 
0038     if (off != 0 || count != real_size)
0039         return -EINVAL;
0040 
0041     mutex_lock(&lua->lua_lock);
0042     retval = roccat_common2_receive(usb_dev, command, buf, real_size);
0043     mutex_unlock(&lua->lua_lock);
0044 
0045     return retval ? retval : real_size;
0046 }
0047 
0048 static ssize_t lua_sysfs_write(struct file *fp, struct kobject *kobj,
0049         void const *buf, loff_t off, size_t count,
0050         size_t real_size, uint command)
0051 {
0052     struct device *dev = kobj_to_dev(kobj);
0053     struct lua_device *lua = hid_get_drvdata(dev_get_drvdata(dev));
0054     struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
0055     int retval;
0056 
0057     if (off != 0 || count != real_size)
0058         return -EINVAL;
0059 
0060     mutex_lock(&lua->lua_lock);
0061     retval = roccat_common2_send(usb_dev, command, buf, real_size);
0062     mutex_unlock(&lua->lua_lock);
0063 
0064     return retval ? retval : real_size;
0065 }
0066 
0067 #define LUA_SYSFS_W(thingy, THINGY) \
0068 static ssize_t lua_sysfs_write_ ## thingy(struct file *fp, \
0069         struct kobject *kobj, struct bin_attribute *attr, \
0070         char *buf, loff_t off, size_t count) \
0071 { \
0072     return lua_sysfs_write(fp, kobj, buf, off, count, \
0073             LUA_SIZE_ ## THINGY, LUA_COMMAND_ ## THINGY); \
0074 }
0075 
0076 #define LUA_SYSFS_R(thingy, THINGY) \
0077 static ssize_t lua_sysfs_read_ ## thingy(struct file *fp, \
0078         struct kobject *kobj, struct bin_attribute *attr, \
0079         char *buf, loff_t off, size_t count) \
0080 { \
0081     return lua_sysfs_read(fp, kobj, buf, off, count, \
0082             LUA_SIZE_ ## THINGY, LUA_COMMAND_ ## THINGY); \
0083 }
0084 
0085 #define LUA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
0086 LUA_SYSFS_W(thingy, THINGY) \
0087 LUA_SYSFS_R(thingy, THINGY) \
0088 static struct bin_attribute lua_ ## thingy ## _attr = { \
0089     .attr = { .name = #thingy, .mode = 0660 }, \
0090     .size = LUA_SIZE_ ## THINGY, \
0091     .read = lua_sysfs_read_ ## thingy, \
0092     .write = lua_sysfs_write_ ## thingy \
0093 };
0094 
0095 LUA_BIN_ATTRIBUTE_RW(control, CONTROL)
0096 
0097 static int lua_create_sysfs_attributes(struct usb_interface *intf)
0098 {
0099     return sysfs_create_bin_file(&intf->dev.kobj, &lua_control_attr);
0100 }
0101 
0102 static void lua_remove_sysfs_attributes(struct usb_interface *intf)
0103 {
0104     sysfs_remove_bin_file(&intf->dev.kobj, &lua_control_attr);
0105 }
0106 
0107 static int lua_init_lua_device_struct(struct usb_device *usb_dev,
0108         struct lua_device *lua)
0109 {
0110     mutex_init(&lua->lua_lock);
0111 
0112     return 0;
0113 }
0114 
0115 static int lua_init_specials(struct hid_device *hdev)
0116 {
0117     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0118     struct usb_device *usb_dev = interface_to_usbdev(intf);
0119     struct lua_device *lua;
0120     int retval;
0121 
0122     lua = kzalloc(sizeof(*lua), GFP_KERNEL);
0123     if (!lua) {
0124         hid_err(hdev, "can't alloc device descriptor\n");
0125         return -ENOMEM;
0126     }
0127     hid_set_drvdata(hdev, lua);
0128 
0129     retval = lua_init_lua_device_struct(usb_dev, lua);
0130     if (retval) {
0131         hid_err(hdev, "couldn't init struct lua_device\n");
0132         goto exit;
0133     }
0134 
0135     retval = lua_create_sysfs_attributes(intf);
0136     if (retval) {
0137         hid_err(hdev, "cannot create sysfs files\n");
0138         goto exit;
0139     }
0140 
0141     return 0;
0142 exit:
0143     kfree(lua);
0144     return retval;
0145 }
0146 
0147 static void lua_remove_specials(struct hid_device *hdev)
0148 {
0149     struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
0150     struct lua_device *lua;
0151 
0152     lua_remove_sysfs_attributes(intf);
0153 
0154     lua = hid_get_drvdata(hdev);
0155     kfree(lua);
0156 }
0157 
0158 static int lua_probe(struct hid_device *hdev,
0159         const struct hid_device_id *id)
0160 {
0161     int retval;
0162 
0163     if (!hid_is_usb(hdev))
0164         return -EINVAL;
0165 
0166     retval = hid_parse(hdev);
0167     if (retval) {
0168         hid_err(hdev, "parse failed\n");
0169         goto exit;
0170     }
0171 
0172     retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
0173     if (retval) {
0174         hid_err(hdev, "hw start failed\n");
0175         goto exit;
0176     }
0177 
0178     retval = lua_init_specials(hdev);
0179     if (retval) {
0180         hid_err(hdev, "couldn't install mouse\n");
0181         goto exit_stop;
0182     }
0183 
0184     return 0;
0185 
0186 exit_stop:
0187     hid_hw_stop(hdev);
0188 exit:
0189     return retval;
0190 }
0191 
0192 static void lua_remove(struct hid_device *hdev)
0193 {
0194     lua_remove_specials(hdev);
0195     hid_hw_stop(hdev);
0196 }
0197 
0198 static const struct hid_device_id lua_devices[] = {
0199     { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_LUA) },
0200     { }
0201 };
0202 
0203 MODULE_DEVICE_TABLE(hid, lua_devices);
0204 
0205 static struct hid_driver lua_driver = {
0206         .name = "lua",
0207         .id_table = lua_devices,
0208         .probe = lua_probe,
0209         .remove = lua_remove
0210 };
0211 module_hid_driver(lua_driver);
0212 
0213 MODULE_AUTHOR("Stefan Achatz");
0214 MODULE_DESCRIPTION("USB Roccat Lua driver");
0215 MODULE_LICENSE("GPL v2");