blob: 8155ac5fede259b0b949793a356f1662a2778f46 [file] [log] [blame]
Stefan Achatz5772f632011-01-30 13:38:23 +01001/*
2 * Roccat common functions for device specific drivers
3 *
4 * Copyright (c) 2011 Stefan Achatz <erazor_de@users.sourceforge.net>
5 */
6
7/*
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the Free
10 * Software Foundation; either version 2 of the License, or (at your option)
11 * any later version.
12 */
13
Stefan Achatz1edd5b42011-06-01 15:54:17 +020014#include <linux/hid.h>
Stefan Achatz5772f632011-01-30 13:38:23 +010015#include <linux/slab.h>
Paul Gortmaker8f86a2c2011-07-03 13:39:48 -040016#include <linux/module.h>
Stefan Achatz5772f632011-01-30 13:38:23 +010017#include "hid-roccat-common.h"
18
Stefan Achatz7392d732012-05-20 22:45:04 +020019static inline uint16_t roccat_common2_feature_report(uint8_t report_id)
Stefan Achatz1edd5b42011-06-01 15:54:17 +020020{
21 return 0x300 | report_id;
22}
23
Stefan Achatz7392d732012-05-20 22:45:04 +020024int roccat_common2_receive(struct usb_device *usb_dev, uint report_id,
Stefan Achatz5772f632011-01-30 13:38:23 +010025 void *data, uint size)
26{
27 char *buf;
28 int len;
29
30 buf = kmalloc(size, GFP_KERNEL);
31 if (buf == NULL)
32 return -ENOMEM;
33
34 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020035 HID_REQ_GET_REPORT,
Stefan Achatz5772f632011-01-30 13:38:23 +010036 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
Stefan Achatz7392d732012-05-20 22:45:04 +020037 roccat_common2_feature_report(report_id),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020038 0, buf, size, USB_CTRL_SET_TIMEOUT);
Stefan Achatz5772f632011-01-30 13:38:23 +010039
40 memcpy(data, buf, size);
41 kfree(buf);
42 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
43}
Stefan Achatz7392d732012-05-20 22:45:04 +020044EXPORT_SYMBOL_GPL(roccat_common2_receive);
Stefan Achatz5772f632011-01-30 13:38:23 +010045
Stefan Achatz7392d732012-05-20 22:45:04 +020046int roccat_common2_send(struct usb_device *usb_dev, uint report_id,
Stefan Achatz5772f632011-01-30 13:38:23 +010047 void const *data, uint size)
48{
49 char *buf;
50 int len;
51
Thomas Meyer4c33a882011-11-17 23:43:40 +010052 buf = kmemdup(data, size, GFP_KERNEL);
Stefan Achatz5772f632011-01-30 13:38:23 +010053 if (buf == NULL)
54 return -ENOMEM;
55
Stefan Achatz5772f632011-01-30 13:38:23 +010056 len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020057 HID_REQ_SET_REPORT,
Stefan Achatz5772f632011-01-30 13:38:23 +010058 USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
Stefan Achatz7392d732012-05-20 22:45:04 +020059 roccat_common2_feature_report(report_id),
Stefan Achatz1edd5b42011-06-01 15:54:17 +020060 0, buf, size, USB_CTRL_SET_TIMEOUT);
Stefan Achatz5772f632011-01-30 13:38:23 +010061
62 kfree(buf);
63 return ((len < 0) ? len : ((len != size) ? -EIO : 0));
64}
Stefan Achatz7392d732012-05-20 22:45:04 +020065EXPORT_SYMBOL_GPL(roccat_common2_send);
Stefan Achatz5772f632011-01-30 13:38:23 +010066
Stefan Achatz7392d732012-05-20 22:45:04 +020067enum roccat_common2_control_states {
Stefan Achatz14fc4292013-10-28 18:52:03 +010068 ROCCAT_COMMON_CONTROL_STATUS_CRITICAL = 0,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020069 ROCCAT_COMMON_CONTROL_STATUS_OK = 1,
70 ROCCAT_COMMON_CONTROL_STATUS_INVALID = 2,
Stefan Achatz14fc4292013-10-28 18:52:03 +010071 ROCCAT_COMMON_CONTROL_STATUS_BUSY = 3,
72 ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW = 4,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020073};
74
Stefan Achatz7392d732012-05-20 22:45:04 +020075static int roccat_common2_receive_control_status(struct usb_device *usb_dev)
Stefan Achatz4728f2d2012-05-20 22:44:59 +020076{
77 int retval;
Stefan Achatz7392d732012-05-20 22:45:04 +020078 struct roccat_common2_control control;
Stefan Achatz4728f2d2012-05-20 22:44:59 +020079
80 do {
81 msleep(50);
Stefan Achatz7392d732012-05-20 22:45:04 +020082 retval = roccat_common2_receive(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020083 ROCCAT_COMMON_COMMAND_CONTROL,
Stefan Achatz7392d732012-05-20 22:45:04 +020084 &control, sizeof(struct roccat_common2_control));
Stefan Achatz4728f2d2012-05-20 22:44:59 +020085
86 if (retval)
87 return retval;
88
89 switch (control.value) {
90 case ROCCAT_COMMON_CONTROL_STATUS_OK:
91 return 0;
Stefan Achatz14fc4292013-10-28 18:52:03 +010092 case ROCCAT_COMMON_CONTROL_STATUS_BUSY:
Stefan Achatz4728f2d2012-05-20 22:44:59 +020093 msleep(500);
94 continue;
95 case ROCCAT_COMMON_CONTROL_STATUS_INVALID:
Stefan Achatz14fc4292013-10-28 18:52:03 +010096 case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL:
97 case ROCCAT_COMMON_CONTROL_STATUS_CRITICAL_NEW:
Stefan Achatz4728f2d2012-05-20 22:44:59 +020098 return -EINVAL;
99 default:
100 dev_err(&usb_dev->dev,
Stefan Achatz7392d732012-05-20 22:45:04 +0200101 "roccat_common2_receive_control_status: "
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200102 "unknown response value 0x%x\n",
103 control.value);
104 return -EINVAL;
105 }
106
107 } while (1);
108}
109
Stefan Achatz7392d732012-05-20 22:45:04 +0200110int roccat_common2_send_with_status(struct usb_device *usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200111 uint command, void const *buf, uint size)
112{
113 int retval;
114
Stefan Achatz7392d732012-05-20 22:45:04 +0200115 retval = roccat_common2_send(usb_dev, command, buf, size);
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200116 if (retval)
117 return retval;
118
119 msleep(100);
120
Stefan Achatz7392d732012-05-20 22:45:04 +0200121 return roccat_common2_receive_control_status(usb_dev);
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200122}
Stefan Achatz7392d732012-05-20 22:45:04 +0200123EXPORT_SYMBOL_GPL(roccat_common2_send_with_status);
Stefan Achatz4728f2d2012-05-20 22:44:59 +0200124
Stefan Achatz71304f52013-10-28 18:52:05 +0100125int roccat_common2_device_init_struct(struct usb_device *usb_dev,
126 struct roccat_common2_device *dev)
127{
128 mutex_init(&dev->lock);
129 return 0;
130}
131EXPORT_SYMBOL_GPL(roccat_common2_device_init_struct);
132
133ssize_t roccat_common2_sysfs_read(struct file *fp, struct kobject *kobj,
134 char *buf, loff_t off, size_t count,
135 size_t real_size, uint command)
136{
Geliang Tang2cf83832015-12-27 17:25:24 +0800137 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatz71304f52013-10-28 18:52:05 +0100138 struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev));
139 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
140 int retval;
141
142 if (off >= real_size)
143 return 0;
144
145 if (off != 0 || count != real_size)
146 return -EINVAL;
147
148 mutex_lock(&roccat_dev->lock);
149 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
150 mutex_unlock(&roccat_dev->lock);
151
152 return retval ? retval : real_size;
153}
154EXPORT_SYMBOL_GPL(roccat_common2_sysfs_read);
155
156ssize_t roccat_common2_sysfs_write(struct file *fp, struct kobject *kobj,
157 void const *buf, loff_t off, size_t count,
158 size_t real_size, uint command)
159{
Geliang Tang2cf83832015-12-27 17:25:24 +0800160 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatz71304f52013-10-28 18:52:05 +0100161 struct roccat_common2_device *roccat_dev = hid_get_drvdata(dev_get_drvdata(dev));
162 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
163 int retval;
164
165 if (off != 0 || count != real_size)
166 return -EINVAL;
167
168 mutex_lock(&roccat_dev->lock);
169 retval = roccat_common2_send_with_status(usb_dev, command, buf, real_size);
170 mutex_unlock(&roccat_dev->lock);
171
172 return retval ? retval : real_size;
173}
174EXPORT_SYMBOL_GPL(roccat_common2_sysfs_write);
175
Stefan Achatz5772f632011-01-30 13:38:23 +0100176MODULE_AUTHOR("Stefan Achatz");
177MODULE_DESCRIPTION("USB Roccat common driver");
178MODULE_LICENSE("GPL v2");