xref: /OK3568_Linux_fs/kernel/drivers/hid/hid-roccat-common.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Roccat common functions for device specific drivers
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun 
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun  */
10*4882a593Smuzhiyun 
11*4882a593Smuzhiyun #include <linux/hid.h>
12*4882a593Smuzhiyun #include <linux/slab.h>
13*4882a593Smuzhiyun #include <linux/module.h>
14*4882a593Smuzhiyun #include "hid-roccat-common.h"
15*4882a593Smuzhiyun 
roccat_common2_feature_report(uint8_t report_id)16*4882a593Smuzhiyun static inline uint16_t roccat_common2_feature_report(uint8_t report_id)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	return 0x300 | report_id;
19*4882a593Smuzhiyun }
20*4882a593Smuzhiyun 
roccat_common2_receive(struct usb_device * usb_dev,uint report_id,void * data,uint size)21*4882a593Smuzhiyun int roccat_common2_receive(struct usb_device *usb_dev, uint report_id,
22*4882a593Smuzhiyun 		void *data, uint size)
23*4882a593Smuzhiyun {
24*4882a593Smuzhiyun 	char *buf;
25*4882a593Smuzhiyun 	int len;
26*4882a593Smuzhiyun 
27*4882a593Smuzhiyun 	buf = kmalloc(size, GFP_KERNEL);
28*4882a593Smuzhiyun 	if (buf == NULL)
29*4882a593Smuzhiyun 		return -ENOMEM;
30*4882a593Smuzhiyun 
31*4882a593Smuzhiyun 	len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
32*4882a593Smuzhiyun 			HID_REQ_GET_REPORT,
33*4882a593Smuzhiyun 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
34*4882a593Smuzhiyun 			roccat_common2_feature_report(report_id),
35*4882a593Smuzhiyun 			0, buf, size, USB_CTRL_SET_TIMEOUT);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	memcpy(data, buf, size);
38*4882a593Smuzhiyun 	kfree(buf);
39*4882a593Smuzhiyun 	return ((len < 0) ? len : ((len != size) ? -EIO : 0));
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(roccat_common2_receive);
42*4882a593Smuzhiyun 
roccat_common2_send(struct usb_device * usb_dev,uint report_id,void const * data,uint size)43*4882a593Smuzhiyun int roccat_common2_send(struct usb_device *usb_dev, uint report_id,
44*4882a593Smuzhiyun 		void const *data, uint size)
45*4882a593Smuzhiyun {
46*4882a593Smuzhiyun 	char *buf;
47*4882a593Smuzhiyun 	int len;
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	buf = kmemdup(data, size, GFP_KERNEL);
50*4882a593Smuzhiyun 	if (buf == NULL)
51*4882a593Smuzhiyun 		return -ENOMEM;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
54*4882a593Smuzhiyun 			HID_REQ_SET_REPORT,
55*4882a593Smuzhiyun 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
56*4882a593Smuzhiyun 			roccat_common2_feature_report(report_id),
57*4882a593Smuzhiyun 			0, buf, size, USB_CTRL_SET_TIMEOUT);
58*4882a593Smuzhiyun 
59*4882a593Smuzhiyun 	kfree(buf);
60*4882a593Smuzhiyun 	return ((len < 0) ? len : ((len != size) ? -EIO : 0));
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(roccat_common2_send);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun enum roccat_common2_control_states {
65*4882a593Smuzhiyun 	ROCCAT_COMMON_CONTROL_STATUS_CRITICAL = 0,
66*4882a593Smuzhiyun 	ROCCAT_COMMON_CONTROL_STATUS_OK = 1,
67*4882a593Smuzhiyun 	ROCCAT_COMMON_CONTROL_STATUS_INVALID = 2,
68*4882a593Smuzhiyun 	ROCCAT_COMMON_CONTROL_STATUS_BUSY = 3,
69*4882a593Smuzhiyun 	ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW = 4,
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
roccat_common2_receive_control_status(struct usb_device * usb_dev)72*4882a593Smuzhiyun static int roccat_common2_receive_control_status(struct usb_device *usb_dev)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	int retval;
75*4882a593Smuzhiyun 	struct roccat_common2_control control;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	do {
78*4882a593Smuzhiyun 		msleep(50);
79*4882a593Smuzhiyun 		retval = roccat_common2_receive(usb_dev,
80*4882a593Smuzhiyun 				ROCCAT_COMMON_COMMAND_CONTROL,
81*4882a593Smuzhiyun 				&control, sizeof(struct roccat_common2_control));
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 		if (retval)
84*4882a593Smuzhiyun 			return retval;
85*4882a593Smuzhiyun 
86*4882a593Smuzhiyun 		switch (control.value) {
87*4882a593Smuzhiyun 		case ROCCAT_COMMON_CONTROL_STATUS_OK:
88*4882a593Smuzhiyun 			return 0;
89*4882a593Smuzhiyun 		case ROCCAT_COMMON_CONTROL_STATUS_BUSY:
90*4882a593Smuzhiyun 			msleep(500);
91*4882a593Smuzhiyun 			continue;
92*4882a593Smuzhiyun 		case ROCCAT_COMMON_CONTROL_STATUS_INVALID:
93*4882a593Smuzhiyun 		case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL:
94*4882a593Smuzhiyun 		case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW:
95*4882a593Smuzhiyun 			return -EINVAL;
96*4882a593Smuzhiyun 		default:
97*4882a593Smuzhiyun 			dev_err(&usb_dev->dev,
98*4882a593Smuzhiyun 					"roccat_common2_receive_control_status: "
99*4882a593Smuzhiyun 					"unknown response value 0x%x\n",
100*4882a593Smuzhiyun 					control.value);
101*4882a593Smuzhiyun 			return -EINVAL;
102*4882a593Smuzhiyun 		}
103*4882a593Smuzhiyun 
104*4882a593Smuzhiyun 	} while (1);
105*4882a593Smuzhiyun }
106*4882a593Smuzhiyun 
roccat_common2_send_with_status(struct usb_device * usb_dev,uint command,void const * buf,uint size)107*4882a593Smuzhiyun int roccat_common2_send_with_status(struct usb_device *usb_dev,
108*4882a593Smuzhiyun 		uint command, void const *buf, uint size)
109*4882a593Smuzhiyun {
110*4882a593Smuzhiyun 	int retval;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	retval = roccat_common2_send(usb_dev, command, buf, size);
113*4882a593Smuzhiyun 	if (retval)
114*4882a593Smuzhiyun 		return retval;
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	msleep(100);
117*4882a593Smuzhiyun 
118*4882a593Smuzhiyun 	return roccat_common2_receive_control_status(usb_dev);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(roccat_common2_send_with_status);
121*4882a593Smuzhiyun 
roccat_common2_device_init_struct(struct usb_device * usb_dev,struct roccat_common2_device * dev)122*4882a593Smuzhiyun int roccat_common2_device_init_struct(struct usb_device *usb_dev,
123*4882a593Smuzhiyun 		struct roccat_common2_device *dev)
124*4882a593Smuzhiyun {
125*4882a593Smuzhiyun 	mutex_init(&dev->lock);
126*4882a593Smuzhiyun 	return 0;
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(roccat_common2_device_init_struct);
129*4882a593Smuzhiyun 
roccat_common2_sysfs_read(struct file * fp,struct kobject * kobj,char * buf,loff_t off,size_t count,size_t real_size,uint command)130*4882a593Smuzhiyun ssize_t roccat_common2_sysfs_read(struct file *fp, struct kobject *kobj,
131*4882a593Smuzhiyun 		char *buf, loff_t off, size_t count,
132*4882a593Smuzhiyun 		size_t real_size, uint command)
133*4882a593Smuzhiyun {
134*4882a593Smuzhiyun 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
135*4882a593Smuzhiyun 	struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev));
136*4882a593Smuzhiyun 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
137*4882a593Smuzhiyun 	int retval;
138*4882a593Smuzhiyun 
139*4882a593Smuzhiyun 	if (off >= real_size)
140*4882a593Smuzhiyun 		return 0;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 	if (off != 0 || count != real_size)
143*4882a593Smuzhiyun 		return -EINVAL;
144*4882a593Smuzhiyun 
145*4882a593Smuzhiyun 	mutex_lock(&roccat_dev->lock);
146*4882a593Smuzhiyun 	retval = roccat_common2_receive(usb_dev, command, buf, real_size);
147*4882a593Smuzhiyun 	mutex_unlock(&roccat_dev->lock);
148*4882a593Smuzhiyun 
149*4882a593Smuzhiyun 	return retval ? retval : real_size;
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(roccat_common2_sysfs_read);
152*4882a593Smuzhiyun 
roccat_common2_sysfs_write(struct file * fp,struct kobject * kobj,void const * buf,loff_t off,size_t count,size_t real_size,uint command)153*4882a593Smuzhiyun ssize_t roccat_common2_sysfs_write(struct file *fp, struct kobject *kobj,
154*4882a593Smuzhiyun 		void const *buf, loff_t off, size_t count,
155*4882a593Smuzhiyun 		size_t real_size, uint command)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	struct device *dev = kobj_to_dev(kobj)->parent->parent;
158*4882a593Smuzhiyun 	struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev));
159*4882a593Smuzhiyun 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
160*4882a593Smuzhiyun 	int retval;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	if (off != 0 || count != real_size)
163*4882a593Smuzhiyun 		return -EINVAL;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	mutex_lock(&roccat_dev->lock);
166*4882a593Smuzhiyun 	retval = roccat_common2_send_with_status(usb_dev, command, buf, real_size);
167*4882a593Smuzhiyun 	mutex_unlock(&roccat_dev->lock);
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	return retval ? retval : real_size;
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(roccat_common2_sysfs_write);
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun MODULE_AUTHOR("Stefan Achatz");
174*4882a593Smuzhiyun MODULE_DESCRIPTION("USB Roccat common driver");
175*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
176