blob: 0332267199d5bd058becaf8c2d295628e0ba29ac [file] [log] [blame]
Stefan Achatz6a2a6392012-05-20 22:44:54 +02001/*
2 * Roccat Savu driver for Linux
3 *
4 * Copyright (c) 2012 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
14/* Roccat Savu is a gamer mouse with macro keys that can be configured in
15 * 5 profiles.
16 */
17
18#include <linux/device.h>
19#include <linux/input.h>
20#include <linux/hid.h>
21#include <linux/module.h>
22#include <linux/slab.h>
23#include <linux/hid-roccat.h>
24#include "hid-ids.h"
25#include "hid-roccat-common.h"
26#include "hid-roccat-savu.h"
27
28static struct class *savu_class;
29
Stefan Achatz6a2a6392012-05-20 22:44:54 +020030static ssize_t savu_sysfs_read(struct file *fp, struct kobject *kobj,
31 char *buf, loff_t off, size_t count,
32 size_t real_size, uint command)
33{
34 struct device *dev =
35 container_of(kobj, struct device, kobj)->parent->parent;
36 struct savu_device *savu = hid_get_drvdata(dev_get_drvdata(dev));
37 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
38 int retval;
39
40 if (off >= real_size)
41 return 0;
42
43 if (off != 0 || count != real_size)
44 return -EINVAL;
45
46 mutex_lock(&savu->savu_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +020047 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
Stefan Achatz6a2a6392012-05-20 22:44:54 +020048 mutex_unlock(&savu->savu_lock);
49
50 return retval ? retval : real_size;
51}
52
53static ssize_t savu_sysfs_write(struct file *fp, struct kobject *kobj,
54 void const *buf, loff_t off, size_t count,
55 size_t real_size, uint command)
56{
57 struct device *dev =
58 container_of(kobj, struct device, kobj)->parent->parent;
59 struct savu_device *savu = hid_get_drvdata(dev_get_drvdata(dev));
60 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
61 int retval;
62
63 if (off != 0 || count != real_size)
64 return -EINVAL;
65
66 mutex_lock(&savu->savu_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +020067 retval = roccat_common2_send_with_status(usb_dev, command,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020068 (void *)buf, real_size);
Stefan Achatz6a2a6392012-05-20 22:44:54 +020069 mutex_unlock(&savu->savu_lock);
70
71 return retval ? retval : real_size;
72}
73
74#define SAVU_SYSFS_W(thingy, THINGY) \
75static ssize_t savu_sysfs_write_ ## thingy(struct file *fp, \
76 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
77 loff_t off, size_t count) \
78{ \
79 return savu_sysfs_write(fp, kobj, buf, off, count, \
80 SAVU_SIZE_ ## THINGY, SAVU_COMMAND_ ## THINGY); \
81}
82
83#define SAVU_SYSFS_R(thingy, THINGY) \
84static ssize_t savu_sysfs_read_ ## thingy(struct file *fp, \
85 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
86 loff_t off, size_t count) \
87{ \
88 return savu_sysfs_read(fp, kobj, buf, off, count, \
89 SAVU_SIZE_ ## THINGY, SAVU_COMMAND_ ## THINGY); \
90}
91
92#define SAVU_SYSFS_RW(thingy, THINGY) \
93SAVU_SYSFS_W(thingy, THINGY) \
94SAVU_SYSFS_R(thingy, THINGY)
95
96#define SAVU_BIN_ATTRIBUTE_RW(thingy, THINGY) \
Greg Kroah-Hartman08079542013-08-19 21:40:27 -070097SAVU_SYSFS_RW(thingy, THINGY); \
98static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatz6a2a6392012-05-20 22:44:54 +020099 .attr = { .name = #thingy, .mode = 0660 }, \
100 .size = SAVU_SIZE_ ## THINGY, \
101 .read = savu_sysfs_read_ ## thingy, \
102 .write = savu_sysfs_write_ ## thingy \
103}
104
Stefan Achatz6a2a6392012-05-20 22:44:54 +0200105#define SAVU_BIN_ATTRIBUTE_W(thingy, THINGY) \
Greg Kroah-Hartman08079542013-08-19 21:40:27 -0700106SAVU_SYSFS_W(thingy, THINGY); \
107static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatz6a2a6392012-05-20 22:44:54 +0200108 .attr = { .name = #thingy, .mode = 0220 }, \
109 .size = SAVU_SIZE_ ## THINGY, \
110 .write = savu_sysfs_write_ ## thingy \
111}
112
Greg Kroah-Hartman08079542013-08-19 21:40:27 -0700113SAVU_BIN_ATTRIBUTE_W(control, CONTROL);
114SAVU_BIN_ATTRIBUTE_RW(profile, PROFILE);
115SAVU_BIN_ATTRIBUTE_RW(general, GENERAL);
116SAVU_BIN_ATTRIBUTE_RW(buttons, BUTTONS);
117SAVU_BIN_ATTRIBUTE_RW(macro, MACRO);
118SAVU_BIN_ATTRIBUTE_RW(info, INFO);
119SAVU_BIN_ATTRIBUTE_RW(sensor, SENSOR);
Stefan Achatz6a2a6392012-05-20 22:44:54 +0200120
Greg Kroah-Hartman08079542013-08-19 21:40:27 -0700121static struct bin_attribute *savu_bin_attributes[] = {
122 &bin_attr_control,
123 &bin_attr_profile,
124 &bin_attr_general,
125 &bin_attr_buttons,
126 &bin_attr_macro,
127 &bin_attr_info,
128 &bin_attr_sensor,
129 NULL,
130};
131
132static const struct attribute_group savu_group = {
133 .bin_attrs = savu_bin_attributes,
134};
135
136static const struct attribute_group *savu_groups[] = {
137 &savu_group,
138 NULL,
Stefan Achatz6a2a6392012-05-20 22:44:54 +0200139};
140
141static int savu_init_savu_device_struct(struct usb_device *usb_dev,
142 struct savu_device *savu)
143{
144 mutex_init(&savu->savu_lock);
145
146 return 0;
147}
148
149static int savu_init_specials(struct hid_device *hdev)
150{
151 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
152 struct usb_device *usb_dev = interface_to_usbdev(intf);
153 struct savu_device *savu;
154 int retval;
155
156 if (intf->cur_altsetting->desc.bInterfaceProtocol
157 != USB_INTERFACE_PROTOCOL_MOUSE) {
158 hid_set_drvdata(hdev, NULL);
159 return 0;
160 }
161
162 savu = kzalloc(sizeof(*savu), GFP_KERNEL);
163 if (!savu) {
164 hid_err(hdev, "can't alloc device descriptor\n");
165 return -ENOMEM;
166 }
167 hid_set_drvdata(hdev, savu);
168
169 retval = savu_init_savu_device_struct(usb_dev, savu);
170 if (retval) {
171 hid_err(hdev, "couldn't init struct savu_device\n");
172 goto exit_free;
173 }
174
175 retval = roccat_connect(savu_class, hdev,
176 sizeof(struct savu_roccat_report));
177 if (retval < 0) {
178 hid_err(hdev, "couldn't init char dev\n");
179 } else {
180 savu->chrdev_minor = retval;
181 savu->roccat_claimed = 1;
182 }
183
184 return 0;
185exit_free:
186 kfree(savu);
187 return retval;
188}
189
190static void savu_remove_specials(struct hid_device *hdev)
191{
192 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
193 struct savu_device *savu;
194
195 if (intf->cur_altsetting->desc.bInterfaceProtocol
196 != USB_INTERFACE_PROTOCOL_MOUSE)
197 return;
198
199 savu = hid_get_drvdata(hdev);
200 if (savu->roccat_claimed)
201 roccat_disconnect(savu->chrdev_minor);
202 kfree(savu);
203}
204
205static int savu_probe(struct hid_device *hdev,
206 const struct hid_device_id *id)
207{
208 int retval;
209
210 retval = hid_parse(hdev);
211 if (retval) {
212 hid_err(hdev, "parse failed\n");
213 goto exit;
214 }
215
216 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
217 if (retval) {
218 hid_err(hdev, "hw start failed\n");
219 goto exit;
220 }
221
222 retval = savu_init_specials(hdev);
223 if (retval) {
224 hid_err(hdev, "couldn't install mouse\n");
225 goto exit_stop;
226 }
227
228 return 0;
229
230exit_stop:
231 hid_hw_stop(hdev);
232exit:
233 return retval;
234}
235
236static void savu_remove(struct hid_device *hdev)
237{
238 savu_remove_specials(hdev);
239 hid_hw_stop(hdev);
240}
241
242static void savu_report_to_chrdev(struct savu_device const *savu,
243 u8 const *data)
244{
245 struct savu_roccat_report roccat_report;
246 struct savu_mouse_report_special const *special_report;
247
248 if (data[0] != SAVU_MOUSE_REPORT_NUMBER_SPECIAL)
249 return;
250
251 special_report = (struct savu_mouse_report_special const *)data;
252
253 roccat_report.type = special_report->type;
254 roccat_report.data[0] = special_report->data[0];
255 roccat_report.data[1] = special_report->data[1];
256 roccat_report_event(savu->chrdev_minor,
257 (uint8_t const *)&roccat_report);
258}
259
260static int savu_raw_event(struct hid_device *hdev,
261 struct hid_report *report, u8 *data, int size)
262{
263 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
264 struct savu_device *savu = hid_get_drvdata(hdev);
265
266 if (intf->cur_altsetting->desc.bInterfaceProtocol
267 != USB_INTERFACE_PROTOCOL_MOUSE)
268 return 0;
269
270 if (savu == NULL)
271 return 0;
272
273 if (savu->roccat_claimed)
274 savu_report_to_chrdev(savu, data);
275
276 return 0;
277}
278
279static const struct hid_device_id savu_devices[] = {
280 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_SAVU) },
281 { }
282};
283
284MODULE_DEVICE_TABLE(hid, savu_devices);
285
286static struct hid_driver savu_driver = {
287 .name = "savu",
288 .id_table = savu_devices,
289 .probe = savu_probe,
290 .remove = savu_remove,
291 .raw_event = savu_raw_event
292};
293
294static int __init savu_init(void)
295{
296 int retval;
297
298 savu_class = class_create(THIS_MODULE, "savu");
299 if (IS_ERR(savu_class))
300 return PTR_ERR(savu_class);
Greg Kroah-Hartman08079542013-08-19 21:40:27 -0700301 savu_class->dev_groups = savu_groups;
Stefan Achatz6a2a6392012-05-20 22:44:54 +0200302
303 retval = hid_register_driver(&savu_driver);
304 if (retval)
305 class_destroy(savu_class);
306 return retval;
307}
308
309static void __exit savu_exit(void)
310{
311 hid_unregister_driver(&savu_driver);
312 class_destroy(savu_class);
313}
314
315module_init(savu_init);
316module_exit(savu_exit);
317
318MODULE_AUTHOR("Stefan Achatz");
319MODULE_DESCRIPTION("USB Roccat Savu driver");
320MODULE_LICENSE("GPL v2");