blob: 4fcc8e7d276f228cb2c29f99df8e6d5db9eef119 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +02002/*
3 * Roccat Pyra driver for Linux
4 *
5 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
6 */
7
8/*
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +02009 */
10
11/*
12 * Roccat Pyra is a mobile gamer mouse which comes in wired and wireless
13 * variant. Wireless variant is not tested.
14 * Userland tools can be found at http://sourceforge.net/projects/roccat
15 */
16
17#include <linux/device.h>
18#include <linux/input.h>
19#include <linux/hid.h>
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020020#include <linux/module.h>
21#include <linux/slab.h>
Stefan Achatz5dc0c982011-02-03 16:14:43 +010022#include <linux/hid-roccat.h>
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020023#include "hid-ids.h"
Stefan Achatz5772f632011-01-30 13:38:23 +010024#include "hid-roccat-common.h"
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020025#include "hid-roccat-pyra.h"
26
Stefan Achatz14a057f2010-11-26 19:57:38 +000027static uint profile_numbers[5] = {0, 1, 2, 3, 4};
28
Stefan Achatz5012aad2010-11-26 19:57:33 +000029/* pyra_class is used for creating sysfs attributes via roccat char device */
30static struct class *pyra_class;
31
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020032static void profile_activated(struct pyra_device *pyra,
33 unsigned int new_profile)
34{
Dan Carpenter606185b2015-01-09 15:32:31 +030035 if (new_profile >= ARRAY_SIZE(pyra->profile_settings))
36 return;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020037 pyra->actual_profile = new_profile;
38 pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
39}
40
41static int pyra_send_control(struct usb_device *usb_dev, int value,
42 enum pyra_control_requests request)
43{
Stefan Achatz7392d732012-05-20 22:45:04 +020044 struct roccat_common2_control control;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020045
46 if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
47 request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
48 (value < 0 || value > 4))
49 return -EINVAL;
50
Stefan Achatz4728f2d2012-05-20 22:44:59 +020051 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020052 control.value = value;
53 control.request = request;
54
Stefan Achatz7392d732012-05-20 22:45:04 +020055 return roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL,
56 &control, sizeof(struct roccat_common2_control));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020057}
58
59static int pyra_get_profile_settings(struct usb_device *usb_dev,
60 struct pyra_profile_settings *buf, int number)
61{
62 int retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020063 retval = pyra_send_control(usb_dev, number,
64 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020065 if (retval)
66 return retval;
Stefan Achatz7392d732012-05-20 22:45:04 +020067 return roccat_common2_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS,
Stefan Achatzbe343802012-11-11 06:20:58 +010068 buf, PYRA_SIZE_PROFILE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020069}
70
71static int pyra_get_settings(struct usb_device *usb_dev,
72 struct pyra_settings *buf)
73{
Stefan Achatz7392d732012-05-20 22:45:04 +020074 return roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
Stefan Achatzbe343802012-11-11 06:20:58 +010075 buf, PYRA_SIZE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020076}
77
78static int pyra_set_settings(struct usb_device *usb_dev,
79 struct pyra_settings const *settings)
80{
Stefan Achatz7392d732012-05-20 22:45:04 +020081 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020082 PYRA_COMMAND_SETTINGS, settings,
Stefan Achatzbe343802012-11-11 06:20:58 +010083 PYRA_SIZE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020084}
85
Stefan Achatzbe343802012-11-11 06:20:58 +010086static ssize_t pyra_sysfs_read(struct file *fp, struct kobject *kobj,
87 char *buf, loff_t off, size_t count,
88 size_t real_size, uint command)
89{
Geliang Tang2cf83832015-12-27 17:25:24 +080090 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatzbe343802012-11-11 06:20:58 +010091 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
92 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
93 int retval;
94
95 if (off >= real_size)
96 return 0;
97
98 if (off != 0 || count != real_size)
99 return -EINVAL;
100
101 mutex_lock(&pyra->pyra_lock);
102 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
103 mutex_unlock(&pyra->pyra_lock);
104
105 if (retval)
106 return retval;
107
108 return real_size;
109}
110
111static ssize_t pyra_sysfs_write(struct file *fp, struct kobject *kobj,
112 void const *buf, loff_t off, size_t count,
113 size_t real_size, uint command)
114{
Geliang Tang2cf83832015-12-27 17:25:24 +0800115 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatzbe343802012-11-11 06:20:58 +0100116 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
117 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
118 int retval;
119
120 if (off != 0 || count != real_size)
121 return -EINVAL;
122
123 mutex_lock(&pyra->pyra_lock);
124 retval = roccat_common2_send_with_status(usb_dev, command, (void *)buf, real_size);
125 mutex_unlock(&pyra->pyra_lock);
126
127 if (retval)
128 return retval;
129
130 return real_size;
131}
132
133#define PYRA_SYSFS_W(thingy, THINGY) \
134static ssize_t pyra_sysfs_write_ ## thingy(struct file *fp, \
135 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
136 loff_t off, size_t count) \
137{ \
138 return pyra_sysfs_write(fp, kobj, buf, off, count, \
139 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
140}
141
142#define PYRA_SYSFS_R(thingy, THINGY) \
143static ssize_t pyra_sysfs_read_ ## thingy(struct file *fp, \
144 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
145 loff_t off, size_t count) \
146{ \
147 return pyra_sysfs_read(fp, kobj, buf, off, count, \
148 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
149}
150
151#define PYRA_SYSFS_RW(thingy, THINGY) \
152PYRA_SYSFS_W(thingy, THINGY) \
153PYRA_SYSFS_R(thingy, THINGY)
154
155#define PYRA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
Greg Kroah-Hartmana7492452013-07-24 15:05:38 -0700156PYRA_SYSFS_RW(thingy, THINGY); \
157static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatzbe343802012-11-11 06:20:58 +0100158 .attr = { .name = #thingy, .mode = 0660 }, \
159 .size = PYRA_SIZE_ ## THINGY, \
160 .read = pyra_sysfs_read_ ## thingy, \
161 .write = pyra_sysfs_write_ ## thingy \
162}
163
164#define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \
Greg Kroah-Hartmana7492452013-07-24 15:05:38 -0700165PYRA_SYSFS_R(thingy, THINGY); \
166static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatzbe343802012-11-11 06:20:58 +0100167 .attr = { .name = #thingy, .mode = 0440 }, \
168 .size = PYRA_SIZE_ ## THINGY, \
169 .read = pyra_sysfs_read_ ## thingy, \
170}
171
172#define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \
Greg Kroah-Hartmana7492452013-07-24 15:05:38 -0700173PYRA_SYSFS_W(thingy, THINGY); \
174static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatzbe343802012-11-11 06:20:58 +0100175 .attr = { .name = #thingy, .mode = 0220 }, \
176 .size = PYRA_SIZE_ ## THINGY, \
177 .write = pyra_sysfs_write_ ## thingy \
178}
179
Greg Kroah-Hartmana7492452013-07-24 15:05:38 -0700180PYRA_BIN_ATTRIBUTE_W(control, CONTROL);
181PYRA_BIN_ATTRIBUTE_RW(info, INFO);
182PYRA_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
183PYRA_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
Stefan Achatzbe343802012-11-11 06:20:58 +0100184
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200185static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
186 struct kobject *kobj, struct bin_attribute *attr, char *buf,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000187 loff_t off, size_t count)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200188{
Geliang Tang2cf83832015-12-27 17:25:24 +0800189 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatzbe343802012-11-11 06:20:58 +0100190 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
191 ssize_t retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200192
Stefan Achatzbe343802012-11-11 06:20:58 +0100193 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
194 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
195 if (retval)
196 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200197
Stefan Achatzbe343802012-11-11 06:20:58 +0100198 return pyra_sysfs_read(fp, kobj, buf, off, count,
199 PYRA_SIZE_PROFILE_SETTINGS,
200 PYRA_COMMAND_PROFILE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200201}
202
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200203static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
204 struct kobject *kobj, struct bin_attribute *attr, char *buf,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000205 loff_t off, size_t count)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200206{
Geliang Tang2cf83832015-12-27 17:25:24 +0800207 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200208 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatzbe343802012-11-11 06:20:58 +0100209 ssize_t retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200210
Stefan Achatzbe343802012-11-11 06:20:58 +0100211 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
212 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200213 if (retval)
214 return retval;
215
Stefan Achatzbe343802012-11-11 06:20:58 +0100216 return pyra_sysfs_read(fp, kobj, buf, off, count,
217 PYRA_SIZE_PROFILE_BUTTONS,
218 PYRA_COMMAND_PROFILE_BUTTONS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200219}
220
Greg Kroah-Hartmana7492452013-07-24 15:05:38 -0700221#define PROFILE_ATTR(number) \
222static struct bin_attribute bin_attr_profile##number##_settings = { \
Stefan Achatz550dbf42013-09-28 05:57:46 +0200223 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
Greg Kroah-Hartmana7492452013-07-24 15:05:38 -0700224 .size = PYRA_SIZE_PROFILE_SETTINGS, \
225 .read = pyra_sysfs_read_profilex_settings, \
226 .private = &profile_numbers[number-1], \
227}; \
228static struct bin_attribute bin_attr_profile##number##_buttons = { \
Stefan Achatz550dbf42013-09-28 05:57:46 +0200229 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
Greg Kroah-Hartmana7492452013-07-24 15:05:38 -0700230 .size = PYRA_SIZE_PROFILE_BUTTONS, \
231 .read = pyra_sysfs_read_profilex_buttons, \
232 .private = &profile_numbers[number-1], \
233};
234PROFILE_ATTR(1);
235PROFILE_ATTR(2);
236PROFILE_ATTR(3);
237PROFILE_ATTR(4);
238PROFILE_ATTR(5);
239
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200240static ssize_t pyra_sysfs_write_settings(struct file *fp,
241 struct kobject *kobj, struct bin_attribute *attr, char *buf,
242 loff_t off, size_t count)
243{
Geliang Tang2cf83832015-12-27 17:25:24 +0800244 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200245 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
246 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
247 int retval = 0;
Stefan Achatzdc186b62011-08-27 15:24:41 +0200248 struct pyra_roccat_report roccat_report;
Stefan Achatzbe343802012-11-11 06:20:58 +0100249 struct pyra_settings const *settings;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200250
Stefan Achatzbe343802012-11-11 06:20:58 +0100251 if (off != 0 || count != PYRA_SIZE_SETTINGS)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200252 return -EINVAL;
253
Stefan Achatzbe343802012-11-11 06:20:58 +0100254 settings = (struct pyra_settings const *)buf;
Dan Carpenter606185b2015-01-09 15:32:31 +0300255 if (settings->startup_profile >= ARRAY_SIZE(pyra->profile_settings))
256 return -EINVAL;
257
258 mutex_lock(&pyra->pyra_lock);
Stefan Achatzdc186b62011-08-27 15:24:41 +0200259
Stefan Achatzbe343802012-11-11 06:20:58 +0100260 retval = pyra_set_settings(usb_dev, settings);
261 if (retval) {
262 mutex_unlock(&pyra->pyra_lock);
263 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200264 }
Stefan Achatzbe343802012-11-11 06:20:58 +0100265
266 profile_activated(pyra, settings->startup_profile);
267
268 roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
269 roccat_report.value = settings->startup_profile + 1;
270 roccat_report.key = 0;
271 roccat_report_event(pyra->chrdev_minor,
272 (uint8_t const *)&roccat_report);
273
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200274 mutex_unlock(&pyra->pyra_lock);
Stefan Achatzbe343802012-11-11 06:20:58 +0100275 return PYRA_SIZE_SETTINGS;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200276}
277
Greg Kroah-Hartmana7492452013-07-24 15:05:38 -0700278PYRA_SYSFS_R(settings, SETTINGS);
279static struct bin_attribute bin_attr_settings =
280 __BIN_ATTR(settings, (S_IWUSR | S_IRUGO),
281 pyra_sysfs_read_settings, pyra_sysfs_write_settings,
282 PYRA_SIZE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200283
284static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
285 struct device_attribute *attr, char *buf)
286{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000287 struct pyra_device *pyra =
288 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200289 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
290}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700291static DEVICE_ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200292
293static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
294 struct device_attribute *attr, char *buf)
295{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000296 struct pyra_device *pyra =
297 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzbe343802012-11-11 06:20:58 +0100298 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
299 struct pyra_settings settings;
300
301 mutex_lock(&pyra->pyra_lock);
302 roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
303 &settings, PYRA_SIZE_SETTINGS);
304 mutex_unlock(&pyra->pyra_lock);
305
306 return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200307}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700308static DEVICE_ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
309static DEVICE_ATTR(startup_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200310
311static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
312 struct device_attribute *attr, char *buf)
313{
Stefan Achatzbe343802012-11-11 06:20:58 +0100314 struct pyra_device *pyra;
315 struct usb_device *usb_dev;
316 struct pyra_info info;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200317
Stefan Achatzbe343802012-11-11 06:20:58 +0100318 dev = dev->parent->parent;
319 pyra = hid_get_drvdata(dev_get_drvdata(dev));
320 usb_dev = interface_to_usbdev(to_usb_interface(dev));
321
322 mutex_lock(&pyra->pyra_lock);
323 roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
324 &info, PYRA_SIZE_INFO);
325 mutex_unlock(&pyra->pyra_lock);
326
327 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200328}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700329static DEVICE_ATTR(firmware_version, 0440, pyra_sysfs_show_firmware_version,
330 NULL);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200331
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700332static struct attribute *pyra_attrs[] = {
333 &dev_attr_actual_cpi.attr,
334 &dev_attr_actual_profile.attr,
335 &dev_attr_firmware_version.attr,
336 &dev_attr_startup_profile.attr,
337 NULL,
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200338};
339
Greg Kroah-Hartmana7492452013-07-24 15:05:38 -0700340static struct bin_attribute *pyra_bin_attributes[] = {
341 &bin_attr_control,
342 &bin_attr_info,
343 &bin_attr_profile_settings,
344 &bin_attr_profile_buttons,
345 &bin_attr_settings,
346 &bin_attr_profile1_settings,
347 &bin_attr_profile2_settings,
348 &bin_attr_profile3_settings,
349 &bin_attr_profile4_settings,
350 &bin_attr_profile5_settings,
351 &bin_attr_profile1_buttons,
352 &bin_attr_profile2_buttons,
353 &bin_attr_profile3_buttons,
354 &bin_attr_profile4_buttons,
355 &bin_attr_profile5_buttons,
356 NULL,
357};
358
359static const struct attribute_group pyra_group = {
360 .attrs = pyra_attrs,
361 .bin_attrs = pyra_bin_attributes,
362};
363
364static const struct attribute_group *pyra_groups[] = {
365 &pyra_group,
366 NULL,
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200367};
368
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200369static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
370 struct pyra_device *pyra)
371{
Stefan Achatzbe343802012-11-11 06:20:58 +0100372 struct pyra_settings settings;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200373 int retval, i;
374
375 mutex_init(&pyra->pyra_lock);
376
Stefan Achatzbe343802012-11-11 06:20:58 +0100377 retval = pyra_get_settings(usb_dev, &settings);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200378 if (retval)
379 return retval;
380
381 for (i = 0; i < 5; ++i) {
382 retval = pyra_get_profile_settings(usb_dev,
383 &pyra->profile_settings[i], i);
384 if (retval)
385 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200386 }
387
Stefan Achatzbe343802012-11-11 06:20:58 +0100388 profile_activated(pyra, settings.startup_profile);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200389
390 return 0;
391}
392
393static int pyra_init_specials(struct hid_device *hdev)
394{
395 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
396 struct usb_device *usb_dev = interface_to_usbdev(intf);
397 struct pyra_device *pyra;
398 int retval;
399
400 if (intf->cur_altsetting->desc.bInterfaceProtocol
401 == USB_INTERFACE_PROTOCOL_MOUSE) {
402
403 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
404 if (!pyra) {
Joe Perches4291ee32010-12-09 19:29:03 -0800405 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200406 return -ENOMEM;
407 }
408 hid_set_drvdata(hdev, pyra);
409
410 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
411 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800412 hid_err(hdev, "couldn't init struct pyra_device\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200413 goto exit_free;
414 }
415
Stefan Achatz8211e462011-01-30 13:38:25 +0100416 retval = roccat_connect(pyra_class, hdev,
417 sizeof(struct pyra_roccat_report));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200418 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800419 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200420 } else {
421 pyra->chrdev_minor = retval;
422 pyra->roccat_claimed = 1;
423 }
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200424 } else {
425 hid_set_drvdata(hdev, NULL);
426 }
427
428 return 0;
429exit_free:
430 kfree(pyra);
431 return retval;
432}
433
434static void pyra_remove_specials(struct hid_device *hdev)
435{
436 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
437 struct pyra_device *pyra;
438
439 if (intf->cur_altsetting->desc.bInterfaceProtocol
440 == USB_INTERFACE_PROTOCOL_MOUSE) {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200441 pyra = hid_get_drvdata(hdev);
442 if (pyra->roccat_claimed)
443 roccat_disconnect(pyra->chrdev_minor);
444 kfree(hid_get_drvdata(hdev));
445 }
446}
447
448static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
449{
450 int retval;
451
Greg Kroah-Hartman93020952021-12-01 19:35:03 +0100452 if (!hid_is_usb(hdev))
453 return -EINVAL;
454
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200455 retval = hid_parse(hdev);
456 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800457 hid_err(hdev, "parse failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200458 goto exit;
459 }
460
461 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
462 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800463 hid_err(hdev, "hw start failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200464 goto exit;
465 }
466
467 retval = pyra_init_specials(hdev);
468 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800469 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200470 goto exit_stop;
471 }
472 return 0;
473
474exit_stop:
475 hid_hw_stop(hdev);
476exit:
477 return retval;
478}
479
480static void pyra_remove(struct hid_device *hdev)
481{
482 pyra_remove_specials(hdev);
483 hid_hw_stop(hdev);
484}
485
486static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
487 u8 const *data)
488{
489 struct pyra_mouse_event_button const *button_event;
490
491 switch (data[0]) {
492 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
493 button_event = (struct pyra_mouse_event_button const *)data;
494 switch (button_event->type) {
495 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
496 profile_activated(pyra, button_event->data1 - 1);
497 break;
498 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
499 pyra->actual_cpi = button_event->data1;
500 break;
501 }
502 break;
503 }
504}
505
506static void pyra_report_to_chrdev(struct pyra_device const *pyra,
507 u8 const *data)
508{
509 struct pyra_roccat_report roccat_report;
510 struct pyra_mouse_event_button const *button_event;
511
512 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
513 return;
514
515 button_event = (struct pyra_mouse_event_button const *)data;
516
517 switch (button_event->type) {
518 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
519 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
520 roccat_report.type = button_event->type;
521 roccat_report.value = button_event->data1;
522 roccat_report.key = 0;
523 roccat_report_event(pyra->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100524 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200525 break;
526 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
527 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
528 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
529 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
530 roccat_report.type = button_event->type;
531 roccat_report.key = button_event->data1;
Stefan Achatzd2b570a2010-09-01 12:42:23 +0200532 /*
533 * pyra reports profile numbers with range 1-5.
534 * Keeping this behaviour.
535 */
536 roccat_report.value = pyra->actual_profile + 1;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200537 roccat_report_event(pyra->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100538 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200539 }
540 break;
541 }
542}
543
544static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
545 u8 *data, int size)
546{
547 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
548 struct pyra_device *pyra = hid_get_drvdata(hdev);
549
550 if (intf->cur_altsetting->desc.bInterfaceProtocol
551 != USB_INTERFACE_PROTOCOL_MOUSE)
552 return 0;
553
Stefan Achatz901e64d2011-06-12 10:02:44 +0200554 if (pyra == NULL)
555 return 0;
556
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200557 pyra_keep_values_up_to_date(pyra, data);
558
559 if (pyra->roccat_claimed)
560 pyra_report_to_chrdev(pyra, data);
561
562 return 0;
563}
564
565static const struct hid_device_id pyra_devices[] = {
566 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
567 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
Stefan Achatz3fce2242011-03-23 18:11:36 +0100568 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
569 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200570 { }
571};
572
573MODULE_DEVICE_TABLE(hid, pyra_devices);
574
575static struct hid_driver pyra_driver = {
576 .name = "pyra",
577 .id_table = pyra_devices,
578 .probe = pyra_probe,
579 .remove = pyra_remove,
580 .raw_event = pyra_raw_event
581};
582
583static int __init pyra_init(void)
584{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000585 int retval;
586
587 /* class name has to be same as driver name */
588 pyra_class = class_create(THIS_MODULE, "pyra");
589 if (IS_ERR(pyra_class))
590 return PTR_ERR(pyra_class);
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700591 pyra_class->dev_groups = pyra_groups;
Stefan Achatz5012aad2010-11-26 19:57:33 +0000592
593 retval = hid_register_driver(&pyra_driver);
594 if (retval)
595 class_destroy(pyra_class);
596 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200597}
598
599static void __exit pyra_exit(void)
600{
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200601 hid_unregister_driver(&pyra_driver);
Stefan Achatz74b643da2011-01-30 13:38:27 +0100602 class_destroy(pyra_class);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200603}
604
605module_init(pyra_init);
606module_exit(pyra_exit);
607
608MODULE_AUTHOR("Stefan Achatz");
609MODULE_DESCRIPTION("USB Roccat Pyra driver");
610MODULE_LICENSE("GPL v2");