blob: 4465ef95d32adcb45e67435fa75b222703382f5c [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Stefan Achatz5772f632011-01-30 13:38:23 +01002/*
3 * Roccat common functions for device specific drivers
4 *
5 * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
6 */
7
8/*
Stefan Achatz5772f632011-01-30 13:38:23 +01009 */
10
Stefan Achatz1edd5b42011-06-01 15:54:17 +020011#include <linux/hid.h>
Stefan Achatz5772f632011-01-30 13:38:23 +010012#include <linux/slab.h>
Paul Gortmaker8f86a2c2011-07-03 13:39:48 -040013#include <linux/module.h>
Stefan Achatz5772f632011-01-30 13:38:23 +010014#include "hid-roccat-common.h"
15
Stefan Achatz7392d732012-05-20 22:45:04 +020016static inline uint16_t roccat_common2_feature_report(uint8_t report_id)
Stefan Achatz1edd5b42011-06-01 15:54:17 +020017{
18 return 0x300 | report_id;
19}
20
Stefan Achatz7392d732012-05-20 22:45:04 +020021int roccat_common2_receive(struct usb_device *usb_dev, uint report_id,
Stefan Achatz5772f632011-01-30 13:38:23 +010022 void *data, uint size)
23{
24 char *buf;
25 int len;
26
27 buf = kmalloc(size, GFP_KERNEL);
28 if (buf == NULL)
29 return -ENOMEM;
30
31 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020032 HID_REQ_GET_REPORT,
Stefan Achatz5772f632011-01-30 13:38:23 +010033 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Stefan Achatz7392d732012-05-20 22:45:04 +020034 roccat_common2_feature_report(report_id),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020035 0, buf, size, USB_CTRL_SET_TIMEOUT);
Stefan Achatz5772f632011-01-30 13:38:23 +010036
37 memcpy(data, buf, size);
38 kfree(buf);
39 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
40}
Stefan Achatz7392d732012-05-20 22:45:04 +020041EXPORT_SYMBOL_GPL(roccat_common2_receive);
Stefan Achatz5772f632011-01-30 13:38:23 +010042
Stefan Achatz7392d732012-05-20 22:45:04 +020043int roccat_common2_send(struct usb_device *usb_dev, uint report_id,
Stefan Achatz5772f632011-01-30 13:38:23 +010044 void const *data, uint size)
45{
46 char *buf;
47 int len;
48
Thomas Meyer4c33a882011-11-17 23:43:40 +010049 buf = kmemdup(data, size, GFP_KERNEL);
Stefan Achatz5772f632011-01-30 13:38:23 +010050 if (buf == NULL)
51 return -ENOMEM;
52
Stefan Achatz5772f632011-01-30 13:38:23 +010053 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020054 HID_REQ_SET_REPORT,
Stefan Achatz5772f632011-01-30 13:38:23 +010055 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Stefan Achatz7392d732012-05-20 22:45:04 +020056 roccat_common2_feature_report(report_id),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020057 0, buf, size, USB_CTRL_SET_TIMEOUT);
Stefan Achatz5772f632011-01-30 13:38:23 +010058
59 kfree(buf);
60 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
61}
Stefan Achatz7392d732012-05-20 22:45:04 +020062EXPORT_SYMBOL_GPL(roccat_common2_send);
Stefan Achatz5772f632011-01-30 13:38:23 +010063
Stefan Achatz7392d732012-05-20 22:45:04 +020064enum roccat_common2_control_states {
Stefan Achatz14fc4292013-10-28 18:52:03 +010065 ROCCAT_COMMON_CONTROL_STATUS_CRITICAL = 0,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020066 ROCCAT_COMMON_CONTROL_STATUS_OK = 1,
67 ROCCAT_COMMON_CONTROL_STATUS_INVALID = 2,
Stefan Achatz14fc4292013-10-28 18:52:03 +010068 ROCCAT_COMMON_CONTROL_STATUS_BUSY = 3,
69 ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW = 4,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020070};
71
Stefan Achatz7392d732012-05-20 22:45:04 +020072static int roccat_common2_receive_control_status(struct usb_device *usb_dev)
Stefan Achatz4728f2d2012-05-20 22:44:59 +020073{
74 int retval;
Stefan Achatz7392d732012-05-20 22:45:04 +020075 struct roccat_common2_control control;
Stefan Achatz4728f2d2012-05-20 22:44:59 +020076
77 do {
78 msleep(50);
Stefan Achatz7392d732012-05-20 22:45:04 +020079 retval = roccat_common2_receive(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020080 ROCCAT_COMMON_COMMAND_CONTROL,
Stefan Achatz7392d732012-05-20 22:45:04 +020081 &control, sizeof(struct roccat_common2_control));
Stefan Achatz4728f2d2012-05-20 22:44:59 +020082
83 if (retval)
84 return retval;
85
86 switch (control.value) {
87 case ROCCAT_COMMON_CONTROL_STATUS_OK:
88 return 0;
Stefan Achatz14fc4292013-10-28 18:52:03 +010089 case ROCCAT_COMMON_CONTROL_STATUS_BUSY:
Stefan Achatz4728f2d2012-05-20 22:44:59 +020090 msleep(500);
91 continue;
92 case ROCCAT_COMMON_CONTROL_STATUS_INVALID:
Stefan Achatz14fc4292013-10-28 18:52:03 +010093 case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL:
94 case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW:
Stefan Achatz4728f2d2012-05-20 22:44:59 +020095 return -EINVAL;
96 default:
97 dev_err(&usb_dev->dev,
Stefan Achatz7392d732012-05-20 22:45:04 +020098 "roccat_common2_receive_control_status: "
Stefan Achatz4728f2d2012-05-20 22:44:59 +020099 "unknown response value 0x%x\n",
100 control.value);
101 return -EINVAL;
102 }
103
104 } while (1);
105}
106
Stefan Achatz7392d732012-05-20 22:45:04 +0200107int roccat_common2_send_with_status(struct usb_device *usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200108 uint command, void const *buf, uint size)
109{
110 int retval;
111
Stefan Achatz7392d732012-05-20 22:45:04 +0200112 retval = roccat_common2_send(usb_dev, command, buf, size);
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200113 if (retval)
114 return retval;
115
116 msleep(100);
117
Stefan Achatz7392d732012-05-20 22:45:04 +0200118 return roccat_common2_receive_control_status(usb_dev);
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200119}
Stefan Achatz7392d732012-05-20 22:45:04 +0200120EXPORT_SYMBOL_GPL(roccat_common2_send_with_status);
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200121
Stefan Achatz71304f52013-10-28 18:52:05 +0100122int roccat_common2_device_init_struct(struct usb_device *usb_dev,
123 struct roccat_common2_device *dev)
124{
125 mutex_init(&dev->lock);
126 return 0;
127}
128EXPORT_SYMBOL_GPL(roccat_common2_device_init_struct);
129
130ssize_t roccat_common2_sysfs_read(struct file *fp, struct kobject *kobj,
131 char *buf, loff_t off, size_t count,
132 size_t real_size, uint command)
133{
Geliang Tang2cf83832015-12-27 17:25:24 +0800134 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatz71304f52013-10-28 18:52:05 +0100135 struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev));
136 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
137 int retval;
138
139 if (off >= real_size)
140 return 0;
141
142 if (off != 0 || count != real_size)
143 return -EINVAL;
144
145 mutex_lock(&roccat_dev->lock);
146 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
147 mutex_unlock(&roccat_dev->lock);
148
149 return retval ? retval : real_size;
150}
151EXPORT_SYMBOL_GPL(roccat_common2_sysfs_read);
152
153ssize_t roccat_common2_sysfs_write(struct file *fp, struct kobject *kobj,
154 void const *buf, loff_t off, size_t count,
155 size_t real_size, uint command)
156{
Geliang Tang2cf83832015-12-27 17:25:24 +0800157 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatz71304f52013-10-28 18:52:05 +0100158 struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev));
159 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
160 int retval;
161
162 if (off != 0 || count != real_size)
163 return -EINVAL;
164
165 mutex_lock(&roccat_dev->lock);
166 retval = roccat_common2_send_with_status(usb_dev, command, buf, real_size);
167 mutex_unlock(&roccat_dev->lock);
168
169 return retval ? retval : real_size;
170}
171EXPORT_SYMBOL_GPL(roccat_common2_sysfs_write);
172
Stefan Achatz5772f632011-01-30 13:38:23 +0100173MODULE_AUTHOR("Stefan Achatz");
174MODULE_DESCRIPTION("USB Roccat common driver");
175MODULE_LICENSE("GPL v2");