blob: d4f1e3bee5909eed76861acff408b582985462af [file] [log] [blame]
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +02001/*
2 * Roccat Pyra driver for Linux
3 *
4 * Copyright (c) 2010 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/*
15 * Roccat Pyra is a mobile gamer mouse which comes in wired and wireless
16 * variant. Wireless variant is not tested.
17 * Userland tools can be found at http://sourceforge.net/projects/roccat
18 */
19
20#include <linux/device.h>
21#include <linux/input.h>
22#include <linux/hid.h>
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020023#include <linux/module.h>
24#include <linux/slab.h>
Stefan Achatz5dc0c982011-02-03 16:14:43 +010025#include <linux/hid-roccat.h>
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020026#include "hid-ids.h"
Stefan Achatz5772f632011-01-30 13:38:23 +010027#include "hid-roccat-common.h"
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020028#include "hid-roccat-pyra.h"
29
Stefan Achatz14a057f2010-11-26 19:57:38 +000030static uint profile_numbers[5] = {0, 1, 2, 3, 4};
31
Stefan Achatz5012aad2010-11-26 19:57:33 +000032/* pyra_class is used for creating sysfs attributes via roccat char device */
33static struct class *pyra_class;
34
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020035static void profile_activated(struct pyra_device *pyra,
36 unsigned int new_profile)
37{
38 pyra->actual_profile = new_profile;
39 pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
40}
41
42static int pyra_send_control(struct usb_device *usb_dev, int value,
43 enum pyra_control_requests request)
44{
Stefan Achatz7392d732012-05-20 22:45:04 +020045 struct roccat_common2_control control;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020046
47 if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
48 request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
49 (value < 0 || value > 4))
50 return -EINVAL;
51
Stefan Achatz4728f2d2012-05-20 22:44:59 +020052 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020053 control.value = value;
54 control.request = request;
55
Stefan Achatz7392d732012-05-20 22:45:04 +020056 return roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL,
57 &control, sizeof(struct roccat_common2_control));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020058}
59
60static int pyra_get_profile_settings(struct usb_device *usb_dev,
61 struct pyra_profile_settings *buf, int number)
62{
63 int retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020064 retval = pyra_send_control(usb_dev, number,
65 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020066 if (retval)
67 return retval;
Stefan Achatz7392d732012-05-20 22:45:04 +020068 return roccat_common2_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS,
Stefan Achatzbe343802012-11-11 06:20:58 +010069 buf, PYRA_SIZE_PROFILE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020070}
71
72static int pyra_get_settings(struct usb_device *usb_dev,
73 struct pyra_settings *buf)
74{
Stefan Achatz7392d732012-05-20 22:45:04 +020075 return roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
Stefan Achatzbe343802012-11-11 06:20:58 +010076 buf, PYRA_SIZE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020077}
78
79static int pyra_set_settings(struct usb_device *usb_dev,
80 struct pyra_settings const *settings)
81{
Stefan Achatz7392d732012-05-20 22:45:04 +020082 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020083 PYRA_COMMAND_SETTINGS, settings,
Stefan Achatzbe343802012-11-11 06:20:58 +010084 PYRA_SIZE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020085}
86
Stefan Achatzbe343802012-11-11 06:20:58 +010087static ssize_t pyra_sysfs_read(struct file *fp, struct kobject *kobj,
88 char *buf, loff_t off, size_t count,
89 size_t real_size, uint command)
90{
91 struct device *dev =
92 container_of(kobj, struct device, kobj)->parent->parent;
93 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
94 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
95 int retval;
96
97 if (off >= real_size)
98 return 0;
99
100 if (off != 0 || count != real_size)
101 return -EINVAL;
102
103 mutex_lock(&pyra->pyra_lock);
104 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
105 mutex_unlock(&pyra->pyra_lock);
106
107 if (retval)
108 return retval;
109
110 return real_size;
111}
112
113static ssize_t pyra_sysfs_write(struct file *fp, struct kobject *kobj,
114 void const *buf, loff_t off, size_t count,
115 size_t real_size, uint command)
116{
117 struct device *dev =
118 container_of(kobj, struct device, kobj)->parent->parent;
119 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
120 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
121 int retval;
122
123 if (off != 0 || count != real_size)
124 return -EINVAL;
125
126 mutex_lock(&pyra->pyra_lock);
127 retval = roccat_common2_send_with_status(usb_dev, command, (void *)buf, real_size);
128 mutex_unlock(&pyra->pyra_lock);
129
130 if (retval)
131 return retval;
132
133 return real_size;
134}
135
136#define PYRA_SYSFS_W(thingy, THINGY) \
137static ssize_t pyra_sysfs_write_ ## thingy(struct file *fp, \
138 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
139 loff_t off, size_t count) \
140{ \
141 return pyra_sysfs_write(fp, kobj, buf, off, count, \
142 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
143}
144
145#define PYRA_SYSFS_R(thingy, THINGY) \
146static ssize_t pyra_sysfs_read_ ## thingy(struct file *fp, \
147 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
148 loff_t off, size_t count) \
149{ \
150 return pyra_sysfs_read(fp, kobj, buf, off, count, \
151 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
152}
153
154#define PYRA_SYSFS_RW(thingy, THINGY) \
155PYRA_SYSFS_W(thingy, THINGY) \
156PYRA_SYSFS_R(thingy, THINGY)
157
158#define PYRA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
159{ \
160 .attr = { .name = #thingy, .mode = 0660 }, \
161 .size = PYRA_SIZE_ ## THINGY, \
162 .read = pyra_sysfs_read_ ## thingy, \
163 .write = pyra_sysfs_write_ ## thingy \
164}
165
166#define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \
167{ \
168 .attr = { .name = #thingy, .mode = 0440 }, \
169 .size = PYRA_SIZE_ ## THINGY, \
170 .read = pyra_sysfs_read_ ## thingy, \
171}
172
173#define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \
174{ \
175 .attr = { .name = #thingy, .mode = 0220 }, \
176 .size = PYRA_SIZE_ ## THINGY, \
177 .write = pyra_sysfs_write_ ## thingy \
178}
179
Stefan Achatzecbfe7a2012-11-11 06:21:10 +0100180PYRA_SYSFS_W(control, CONTROL)
Stefan Achatzbe343802012-11-11 06:20:58 +0100181PYRA_SYSFS_RW(info, INFO)
Stefan Achatzecbfe7a2012-11-11 06:21:10 +0100182PYRA_SYSFS_RW(profile_settings, PROFILE_SETTINGS)
183PYRA_SYSFS_RW(profile_buttons, PROFILE_BUTTONS)
Stefan Achatzbe343802012-11-11 06:20:58 +0100184PYRA_SYSFS_R(settings, SETTINGS)
185
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200186static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
187 struct kobject *kobj, struct bin_attribute *attr, char *buf,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000188 loff_t off, size_t count)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200189{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000190 struct device *dev =
191 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzbe343802012-11-11 06:20:58 +0100192 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
193 ssize_t retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200194
Stefan Achatzbe343802012-11-11 06:20:58 +0100195 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
196 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
197 if (retval)
198 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200199
Stefan Achatzbe343802012-11-11 06:20:58 +0100200 return pyra_sysfs_read(fp, kobj, buf, off, count,
201 PYRA_SIZE_PROFILE_SETTINGS,
202 PYRA_COMMAND_PROFILE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200203}
204
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200205static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
206 struct kobject *kobj, struct bin_attribute *attr, char *buf,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000207 loff_t off, size_t count)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200208{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000209 struct device *dev =
210 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200211 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatzbe343802012-11-11 06:20:58 +0100212 ssize_t retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200213
Stefan Achatzbe343802012-11-11 06:20:58 +0100214 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
215 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200216 if (retval)
217 return retval;
218
Stefan Achatzbe343802012-11-11 06:20:58 +0100219 return pyra_sysfs_read(fp, kobj, buf, off, count,
220 PYRA_SIZE_PROFILE_BUTTONS,
221 PYRA_COMMAND_PROFILE_BUTTONS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200222}
223
224static ssize_t pyra_sysfs_write_settings(struct file *fp,
225 struct kobject *kobj, struct bin_attribute *attr, char *buf,
226 loff_t off, size_t count)
227{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000228 struct device *dev =
229 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200230 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
231 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
232 int retval = 0;
Stefan Achatzdc186b62011-08-27 15:24:41 +0200233 struct pyra_roccat_report roccat_report;
Stefan Achatzbe343802012-11-11 06:20:58 +0100234 struct pyra_settings const *settings;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200235
Stefan Achatzbe343802012-11-11 06:20:58 +0100236 if (off != 0 || count != PYRA_SIZE_SETTINGS)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200237 return -EINVAL;
238
239 mutex_lock(&pyra->pyra_lock);
Stefan Achatzdc186b62011-08-27 15:24:41 +0200240
Stefan Achatzbe343802012-11-11 06:20:58 +0100241 settings = (struct pyra_settings const *)buf;
Stefan Achatzdc186b62011-08-27 15:24:41 +0200242
Stefan Achatzbe343802012-11-11 06:20:58 +0100243 retval = pyra_set_settings(usb_dev, settings);
244 if (retval) {
245 mutex_unlock(&pyra->pyra_lock);
246 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200247 }
Stefan Achatzbe343802012-11-11 06:20:58 +0100248
249 profile_activated(pyra, settings->startup_profile);
250
251 roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
252 roccat_report.value = settings->startup_profile + 1;
253 roccat_report.key = 0;
254 roccat_report_event(pyra->chrdev_minor,
255 (uint8_t const *)&roccat_report);
256
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200257 mutex_unlock(&pyra->pyra_lock);
Stefan Achatzbe343802012-11-11 06:20:58 +0100258 return PYRA_SIZE_SETTINGS;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200259}
260
261
262static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
263 struct device_attribute *attr, char *buf)
264{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000265 struct pyra_device *pyra =
266 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200267 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
268}
269
270static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
271 struct device_attribute *attr, char *buf)
272{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000273 struct pyra_device *pyra =
274 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzbe343802012-11-11 06:20:58 +0100275 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
276 struct pyra_settings settings;
277
278 mutex_lock(&pyra->pyra_lock);
279 roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
280 &settings, PYRA_SIZE_SETTINGS);
281 mutex_unlock(&pyra->pyra_lock);
282
283 return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200284}
285
286static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
287 struct device_attribute *attr, char *buf)
288{
Stefan Achatzbe343802012-11-11 06:20:58 +0100289 struct pyra_device *pyra;
290 struct usb_device *usb_dev;
291 struct pyra_info info;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200292
Stefan Achatzbe343802012-11-11 06:20:58 +0100293 dev = dev->parent->parent;
294 pyra = hid_get_drvdata(dev_get_drvdata(dev));
295 usb_dev = interface_to_usbdev(to_usb_interface(dev));
296
297 mutex_lock(&pyra->pyra_lock);
298 roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
299 &info, PYRA_SIZE_INFO);
300 mutex_unlock(&pyra->pyra_lock);
301
302 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200303}
304
Stefan Achatz5012aad2010-11-26 19:57:33 +0000305static struct device_attribute pyra_attributes[] = {
306 __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
307 __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
308 __ATTR(firmware_version, 0440,
309 pyra_sysfs_show_firmware_version, NULL),
310 __ATTR(startup_profile, 0440,
Stefan Achatzbe343802012-11-11 06:20:58 +0100311 pyra_sysfs_show_actual_profile, NULL),
Stefan Achatz5012aad2010-11-26 19:57:33 +0000312 __ATTR_NULL
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200313};
314
Stefan Achatz5012aad2010-11-26 19:57:33 +0000315static struct bin_attribute pyra_bin_attributes[] = {
Stefan Achatzecbfe7a2012-11-11 06:21:10 +0100316 PYRA_BIN_ATTRIBUTE_W(control, CONTROL),
Stefan Achatzbe343802012-11-11 06:20:58 +0100317 PYRA_BIN_ATTRIBUTE_RW(info, INFO),
Stefan Achatzecbfe7a2012-11-11 06:21:10 +0100318 PYRA_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS),
319 PYRA_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS),
Stefan Achatzbe343802012-11-11 06:20:58 +0100320 PYRA_BIN_ATTRIBUTE_RW(settings, SETTINGS),
Stefan Achatz5012aad2010-11-26 19:57:33 +0000321 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200322 .attr = { .name = "profile1_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100323 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000324 .read = pyra_sysfs_read_profilex_settings,
325 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000326 },
327 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200328 .attr = { .name = "profile2_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100329 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000330 .read = pyra_sysfs_read_profilex_settings,
331 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000332 },
333 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200334 .attr = { .name = "profile3_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100335 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000336 .read = pyra_sysfs_read_profilex_settings,
337 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000338 },
339 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200340 .attr = { .name = "profile4_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100341 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000342 .read = pyra_sysfs_read_profilex_settings,
343 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000344 },
345 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200346 .attr = { .name = "profile5_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100347 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000348 .read = pyra_sysfs_read_profilex_settings,
349 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000350 },
351 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200352 .attr = { .name = "profile1_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100353 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000354 .read = pyra_sysfs_read_profilex_buttons,
355 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000356 },
357 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200358 .attr = { .name = "profile2_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100359 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000360 .read = pyra_sysfs_read_profilex_buttons,
361 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000362 },
363 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200364 .attr = { .name = "profile3_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100365 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000366 .read = pyra_sysfs_read_profilex_buttons,
367 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000368 },
369 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200370 .attr = { .name = "profile4_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100371 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000372 .read = pyra_sysfs_read_profilex_buttons,
373 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000374 },
375 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200376 .attr = { .name = "profile5_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100377 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000378 .read = pyra_sysfs_read_profilex_buttons,
379 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000380 },
Stefan Achatz5012aad2010-11-26 19:57:33 +0000381 __ATTR_NULL
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200382};
383
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200384static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
385 struct pyra_device *pyra)
386{
Stefan Achatzbe343802012-11-11 06:20:58 +0100387 struct pyra_settings settings;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200388 int retval, i;
389
390 mutex_init(&pyra->pyra_lock);
391
Stefan Achatzbe343802012-11-11 06:20:58 +0100392 retval = pyra_get_settings(usb_dev, &settings);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200393 if (retval)
394 return retval;
395
396 for (i = 0; i < 5; ++i) {
397 retval = pyra_get_profile_settings(usb_dev,
398 &pyra->profile_settings[i], i);
399 if (retval)
400 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200401 }
402
Stefan Achatzbe343802012-11-11 06:20:58 +0100403 profile_activated(pyra, settings.startup_profile);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200404
405 return 0;
406}
407
408static int pyra_init_specials(struct hid_device *hdev)
409{
410 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
411 struct usb_device *usb_dev = interface_to_usbdev(intf);
412 struct pyra_device *pyra;
413 int retval;
414
415 if (intf->cur_altsetting->desc.bInterfaceProtocol
416 == USB_INTERFACE_PROTOCOL_MOUSE) {
417
418 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
419 if (!pyra) {
Joe Perches4291ee32010-12-09 19:29:03 -0800420 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200421 return -ENOMEM;
422 }
423 hid_set_drvdata(hdev, pyra);
424
425 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
426 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800427 hid_err(hdev, "couldn't init struct pyra_device\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200428 goto exit_free;
429 }
430
Stefan Achatz8211e462011-01-30 13:38:25 +0100431 retval = roccat_connect(pyra_class, hdev,
432 sizeof(struct pyra_roccat_report));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200433 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800434 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200435 } else {
436 pyra->chrdev_minor = retval;
437 pyra->roccat_claimed = 1;
438 }
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200439 } else {
440 hid_set_drvdata(hdev, NULL);
441 }
442
443 return 0;
444exit_free:
445 kfree(pyra);
446 return retval;
447}
448
449static void pyra_remove_specials(struct hid_device *hdev)
450{
451 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
452 struct pyra_device *pyra;
453
454 if (intf->cur_altsetting->desc.bInterfaceProtocol
455 == USB_INTERFACE_PROTOCOL_MOUSE) {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200456 pyra = hid_get_drvdata(hdev);
457 if (pyra->roccat_claimed)
458 roccat_disconnect(pyra->chrdev_minor);
459 kfree(hid_get_drvdata(hdev));
460 }
461}
462
463static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
464{
465 int retval;
466
467 retval = hid_parse(hdev);
468 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800469 hid_err(hdev, "parse failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200470 goto exit;
471 }
472
473 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
474 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800475 hid_err(hdev, "hw start failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200476 goto exit;
477 }
478
479 retval = pyra_init_specials(hdev);
480 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800481 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200482 goto exit_stop;
483 }
484 return 0;
485
486exit_stop:
487 hid_hw_stop(hdev);
488exit:
489 return retval;
490}
491
492static void pyra_remove(struct hid_device *hdev)
493{
494 pyra_remove_specials(hdev);
495 hid_hw_stop(hdev);
496}
497
498static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
499 u8 const *data)
500{
501 struct pyra_mouse_event_button const *button_event;
502
503 switch (data[0]) {
504 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
505 button_event = (struct pyra_mouse_event_button const *)data;
506 switch (button_event->type) {
507 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
508 profile_activated(pyra, button_event->data1 - 1);
509 break;
510 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
511 pyra->actual_cpi = button_event->data1;
512 break;
513 }
514 break;
515 }
516}
517
518static void pyra_report_to_chrdev(struct pyra_device const *pyra,
519 u8 const *data)
520{
521 struct pyra_roccat_report roccat_report;
522 struct pyra_mouse_event_button const *button_event;
523
524 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
525 return;
526
527 button_event = (struct pyra_mouse_event_button const *)data;
528
529 switch (button_event->type) {
530 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
531 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
532 roccat_report.type = button_event->type;
533 roccat_report.value = button_event->data1;
534 roccat_report.key = 0;
535 roccat_report_event(pyra->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100536 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200537 break;
538 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
539 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
540 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
541 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
542 roccat_report.type = button_event->type;
543 roccat_report.key = button_event->data1;
Stefan Achatzd2b570a2010-09-01 12:42:23 +0200544 /*
545 * pyra reports profile numbers with range 1-5.
546 * Keeping this behaviour.
547 */
548 roccat_report.value = pyra->actual_profile + 1;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200549 roccat_report_event(pyra->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100550 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200551 }
552 break;
553 }
554}
555
556static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
557 u8 *data, int size)
558{
559 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
560 struct pyra_device *pyra = hid_get_drvdata(hdev);
561
562 if (intf->cur_altsetting->desc.bInterfaceProtocol
563 != USB_INTERFACE_PROTOCOL_MOUSE)
564 return 0;
565
Stefan Achatz901e64d2011-06-12 10:02:44 +0200566 if (pyra == NULL)
567 return 0;
568
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200569 pyra_keep_values_up_to_date(pyra, data);
570
571 if (pyra->roccat_claimed)
572 pyra_report_to_chrdev(pyra, data);
573
574 return 0;
575}
576
577static const struct hid_device_id pyra_devices[] = {
578 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
579 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
Stefan Achatz3fce2242011-03-23 18:11:36 +0100580 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
581 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200582 { }
583};
584
585MODULE_DEVICE_TABLE(hid, pyra_devices);
586
587static struct hid_driver pyra_driver = {
588 .name = "pyra",
589 .id_table = pyra_devices,
590 .probe = pyra_probe,
591 .remove = pyra_remove,
592 .raw_event = pyra_raw_event
593};
594
595static int __init pyra_init(void)
596{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000597 int retval;
598
599 /* class name has to be same as driver name */
600 pyra_class = class_create(THIS_MODULE, "pyra");
601 if (IS_ERR(pyra_class))
602 return PTR_ERR(pyra_class);
603 pyra_class->dev_attrs = pyra_attributes;
604 pyra_class->dev_bin_attrs = pyra_bin_attributes;
605
606 retval = hid_register_driver(&pyra_driver);
607 if (retval)
608 class_destroy(pyra_class);
609 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200610}
611
612static void __exit pyra_exit(void)
613{
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200614 hid_unregister_driver(&pyra_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100615 class_destroy(pyra_class);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200616}
617
618module_init(pyra_init);
619module_exit(pyra_exit);
620
621MODULE_AUTHOR("Stefan Achatz");
622MODULE_DESCRIPTION("USB Roccat Pyra driver");
623MODULE_LICENSE("GPL v2");