xref: /OK3568_Linux_fs/kernel/drivers/hid/hid-roccat-lua.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Roccat Lua driver for Linux
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2012 Stefan Achatz <erazor_de@users.sourceforge.net>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun  * Roccat Lua is a gamer mouse which cpi, button and light settings can be
13*4882a593Smuzhiyun  * configured.
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <linux/device.h>
17*4882a593Smuzhiyun #include <linux/input.h>
18*4882a593Smuzhiyun #include <linux/hid.h>
19*4882a593Smuzhiyun #include <linux/module.h>
20*4882a593Smuzhiyun #include <linux/slab.h>
21*4882a593Smuzhiyun #include <linux/hid-roccat.h>
22*4882a593Smuzhiyun #include "hid-ids.h"
23*4882a593Smuzhiyun #include "hid-roccat-common.h"
24*4882a593Smuzhiyun #include "hid-roccat-lua.h"
25*4882a593Smuzhiyun 
lua_sysfs_read(struct file * fp,struct kobject * kobj,char * buf,loff_t off,size_t count,size_t real_size,uint command)26*4882a593Smuzhiyun static ssize_t lua_sysfs_read(struct file *fp, struct kobject *kobj,
27*4882a593Smuzhiyun 		char *buf, loff_t off, size_t count,
28*4882a593Smuzhiyun 		size_t real_size, uint command)
29*4882a593Smuzhiyun {
30*4882a593Smuzhiyun 	struct device *dev = kobj_to_dev(kobj);
31*4882a593Smuzhiyun 	struct lua_device *lua = hid_get_drvdata(dev_get_drvdata(dev));
32*4882a593Smuzhiyun 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
33*4882a593Smuzhiyun 	int retval;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	if (off >= real_size)
36*4882a593Smuzhiyun 		return 0;
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun 	if (off != 0 || count != real_size)
39*4882a593Smuzhiyun 		return -EINVAL;
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	mutex_lock(&lua->lua_lock);
42*4882a593Smuzhiyun 	retval = roccat_common2_receive(usb_dev, command, buf, real_size);
43*4882a593Smuzhiyun 	mutex_unlock(&lua->lua_lock);
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	return retval ? retval : real_size;
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun 
lua_sysfs_write(struct file * fp,struct kobject * kobj,void const * buf,loff_t off,size_t count,size_t real_size,uint command)48*4882a593Smuzhiyun static ssize_t lua_sysfs_write(struct file *fp, struct kobject *kobj,
49*4882a593Smuzhiyun 		void const *buf, loff_t off, size_t count,
50*4882a593Smuzhiyun 		size_t real_size, uint command)
51*4882a593Smuzhiyun {
52*4882a593Smuzhiyun 	struct device *dev = kobj_to_dev(kobj);
53*4882a593Smuzhiyun 	struct lua_device *lua = hid_get_drvdata(dev_get_drvdata(dev));
54*4882a593Smuzhiyun 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
55*4882a593Smuzhiyun 	int retval;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 	if (off != 0 || count != real_size)
58*4882a593Smuzhiyun 		return -EINVAL;
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	mutex_lock(&lua->lua_lock);
61*4882a593Smuzhiyun 	retval = roccat_common2_send(usb_dev, command, buf, real_size);
62*4882a593Smuzhiyun 	mutex_unlock(&lua->lua_lock);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	return retval ? retval : real_size;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun 
67*4882a593Smuzhiyun #define LUA_SYSFS_W(thingy, THINGY) \
68*4882a593Smuzhiyun static ssize_t lua_sysfs_write_ ## thingy(struct file *fp, \
69*4882a593Smuzhiyun 		struct kobject *kobj, struct bin_attribute *attr, \
70*4882a593Smuzhiyun 		char *buf, loff_t off, size_t count) \
71*4882a593Smuzhiyun { \
72*4882a593Smuzhiyun 	return lua_sysfs_write(fp, kobj, buf, off, count, \
73*4882a593Smuzhiyun 			LUA_SIZE_ ## THINGY, LUA_COMMAND_ ## THINGY); \
74*4882a593Smuzhiyun }
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun #define LUA_SYSFS_R(thingy, THINGY) \
77*4882a593Smuzhiyun static ssize_t lua_sysfs_read_ ## thingy(struct file *fp, \
78*4882a593Smuzhiyun 		struct kobject *kobj, struct bin_attribute *attr, \
79*4882a593Smuzhiyun 		char *buf, loff_t off, size_t count) \
80*4882a593Smuzhiyun { \
81*4882a593Smuzhiyun 	return lua_sysfs_read(fp, kobj, buf, off, count, \
82*4882a593Smuzhiyun 			LUA_SIZE_ ## THINGY, LUA_COMMAND_ ## THINGY); \
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun #define LUA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
86*4882a593Smuzhiyun LUA_SYSFS_W(thingy, THINGY) \
87*4882a593Smuzhiyun LUA_SYSFS_R(thingy, THINGY) \
88*4882a593Smuzhiyun static struct bin_attribute lua_ ## thingy ## _attr = { \
89*4882a593Smuzhiyun 	.attr = { .name = #thingy, .mode = 0660 }, \
90*4882a593Smuzhiyun 	.size = LUA_SIZE_ ## THINGY, \
91*4882a593Smuzhiyun 	.read = lua_sysfs_read_ ## thingy, \
92*4882a593Smuzhiyun 	.write = lua_sysfs_write_ ## thingy \
93*4882a593Smuzhiyun };
94*4882a593Smuzhiyun 
LUA_BIN_ATTRIBUTE_RW(control,CONTROL)95*4882a593Smuzhiyun LUA_BIN_ATTRIBUTE_RW(control, CONTROL)
96*4882a593Smuzhiyun 
97*4882a593Smuzhiyun static int lua_create_sysfs_attributes(struct usb_interface *intf)
98*4882a593Smuzhiyun {
99*4882a593Smuzhiyun 	return sysfs_create_bin_file(&intf->dev.kobj, &lua_control_attr);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun 
lua_remove_sysfs_attributes(struct usb_interface * intf)102*4882a593Smuzhiyun static void lua_remove_sysfs_attributes(struct usb_interface *intf)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	sysfs_remove_bin_file(&intf->dev.kobj, &lua_control_attr);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
lua_init_lua_device_struct(struct usb_device * usb_dev,struct lua_device * lua)107*4882a593Smuzhiyun static int lua_init_lua_device_struct(struct usb_device *usb_dev,
108*4882a593Smuzhiyun 		struct lua_device *lua)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	mutex_init(&lua->lua_lock);
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	return 0;
113*4882a593Smuzhiyun }
114*4882a593Smuzhiyun 
lua_init_specials(struct hid_device * hdev)115*4882a593Smuzhiyun static int lua_init_specials(struct hid_device *hdev)
116*4882a593Smuzhiyun {
117*4882a593Smuzhiyun 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
118*4882a593Smuzhiyun 	struct usb_device *usb_dev = interface_to_usbdev(intf);
119*4882a593Smuzhiyun 	struct lua_device *lua;
120*4882a593Smuzhiyun 	int retval;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	lua = kzalloc(sizeof(*lua), GFP_KERNEL);
123*4882a593Smuzhiyun 	if (!lua) {
124*4882a593Smuzhiyun 		hid_err(hdev, "can't alloc device descriptor\n");
125*4882a593Smuzhiyun 		return -ENOMEM;
126*4882a593Smuzhiyun 	}
127*4882a593Smuzhiyun 	hid_set_drvdata(hdev, lua);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	retval = lua_init_lua_device_struct(usb_dev, lua);
130*4882a593Smuzhiyun 	if (retval) {
131*4882a593Smuzhiyun 		hid_err(hdev, "couldn't init struct lua_device\n");
132*4882a593Smuzhiyun 		goto exit;
133*4882a593Smuzhiyun 	}
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun 	retval = lua_create_sysfs_attributes(intf);
136*4882a593Smuzhiyun 	if (retval) {
137*4882a593Smuzhiyun 		hid_err(hdev, "cannot create sysfs files\n");
138*4882a593Smuzhiyun 		goto exit;
139*4882a593Smuzhiyun 	}
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	return 0;
142*4882a593Smuzhiyun exit:
143*4882a593Smuzhiyun 	kfree(lua);
144*4882a593Smuzhiyun 	return retval;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun 
lua_remove_specials(struct hid_device * hdev)147*4882a593Smuzhiyun static void lua_remove_specials(struct hid_device *hdev)
148*4882a593Smuzhiyun {
149*4882a593Smuzhiyun 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
150*4882a593Smuzhiyun 	struct lua_device *lua;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	lua_remove_sysfs_attributes(intf);
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	lua = hid_get_drvdata(hdev);
155*4882a593Smuzhiyun 	kfree(lua);
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun 
lua_probe(struct hid_device * hdev,const struct hid_device_id * id)158*4882a593Smuzhiyun static int lua_probe(struct hid_device *hdev,
159*4882a593Smuzhiyun 		const struct hid_device_id *id)
160*4882a593Smuzhiyun {
161*4882a593Smuzhiyun 	int retval;
162*4882a593Smuzhiyun 
163*4882a593Smuzhiyun 	if (!hid_is_usb(hdev))
164*4882a593Smuzhiyun 		return -EINVAL;
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	retval = hid_parse(hdev);
167*4882a593Smuzhiyun 	if (retval) {
168*4882a593Smuzhiyun 		hid_err(hdev, "parse failed\n");
169*4882a593Smuzhiyun 		goto exit;
170*4882a593Smuzhiyun 	}
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
173*4882a593Smuzhiyun 	if (retval) {
174*4882a593Smuzhiyun 		hid_err(hdev, "hw start failed\n");
175*4882a593Smuzhiyun 		goto exit;
176*4882a593Smuzhiyun 	}
177*4882a593Smuzhiyun 
178*4882a593Smuzhiyun 	retval = lua_init_specials(hdev);
179*4882a593Smuzhiyun 	if (retval) {
180*4882a593Smuzhiyun 		hid_err(hdev, "couldn't install mouse\n");
181*4882a593Smuzhiyun 		goto exit_stop;
182*4882a593Smuzhiyun 	}
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	return 0;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun exit_stop:
187*4882a593Smuzhiyun 	hid_hw_stop(hdev);
188*4882a593Smuzhiyun exit:
189*4882a593Smuzhiyun 	return retval;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
lua_remove(struct hid_device * hdev)192*4882a593Smuzhiyun static void lua_remove(struct hid_device *hdev)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun 	lua_remove_specials(hdev);
195*4882a593Smuzhiyun 	hid_hw_stop(hdev);
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun static const struct hid_device_id lua_devices[] = {
199*4882a593Smuzhiyun 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_LUA) },
200*4882a593Smuzhiyun 	{ }
201*4882a593Smuzhiyun };
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun MODULE_DEVICE_TABLE(hid, lua_devices);
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun static struct hid_driver lua_driver = {
206*4882a593Smuzhiyun 		.name = "lua",
207*4882a593Smuzhiyun 		.id_table = lua_devices,
208*4882a593Smuzhiyun 		.probe = lua_probe,
209*4882a593Smuzhiyun 		.remove = lua_remove
210*4882a593Smuzhiyun };
211*4882a593Smuzhiyun module_hid_driver(lua_driver);
212*4882a593Smuzhiyun 
213*4882a593Smuzhiyun MODULE_AUTHOR("Stefan Achatz");
214*4882a593Smuzhiyun MODULE_DESCRIPTION("USB Roccat Lua driver");
215*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
216