blob: 76199fa727ed188538ca61ba9eee36e1a042d880 [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
180PYRA_SYSFS_RW(info, INFO)
181PYRA_SYSFS_W(profile_settings, PROFILE_SETTINGS)
182PYRA_SYSFS_W(profile_buttons, PROFILE_BUTTONS)
183PYRA_SYSFS_R(settings, SETTINGS)
184
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{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000189 struct device *dev =
190 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzbe343802012-11-11 06:20:58 +0100191 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
192 ssize_t retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200193
Stefan Achatzbe343802012-11-11 06:20:58 +0100194 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
195 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
196 if (retval)
197 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200198
Stefan Achatzbe343802012-11-11 06:20:58 +0100199 return pyra_sysfs_read(fp, kobj, buf, off, count,
200 PYRA_SIZE_PROFILE_SETTINGS,
201 PYRA_COMMAND_PROFILE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200202}
203
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200204static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
205 struct kobject *kobj, struct bin_attribute *attr, char *buf,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000206 loff_t off, size_t count)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200207{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000208 struct device *dev =
209 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200210 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
Stefan Achatzbe343802012-11-11 06:20:58 +0100211 ssize_t retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200212
Stefan Achatzbe343802012-11-11 06:20:58 +0100213 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
214 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200215 if (retval)
216 return retval;
217
Stefan Achatzbe343802012-11-11 06:20:58 +0100218 return pyra_sysfs_read(fp, kobj, buf, off, count,
219 PYRA_SIZE_PROFILE_BUTTONS,
220 PYRA_COMMAND_PROFILE_BUTTONS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200221}
222
223static ssize_t pyra_sysfs_write_settings(struct file *fp,
224 struct kobject *kobj, struct bin_attribute *attr, char *buf,
225 loff_t off, size_t count)
226{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000227 struct device *dev =
228 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200229 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
230 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
231 int retval = 0;
Stefan Achatzdc186b62011-08-27 15:24:41 +0200232 struct pyra_roccat_report roccat_report;
Stefan Achatzbe343802012-11-11 06:20:58 +0100233 struct pyra_settings const *settings;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200234
Stefan Achatzbe343802012-11-11 06:20:58 +0100235 if (off != 0 || count != PYRA_SIZE_SETTINGS)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200236 return -EINVAL;
237
238 mutex_lock(&pyra->pyra_lock);
Stefan Achatzdc186b62011-08-27 15:24:41 +0200239
Stefan Achatzbe343802012-11-11 06:20:58 +0100240 settings = (struct pyra_settings const *)buf;
Stefan Achatzdc186b62011-08-27 15:24:41 +0200241
Stefan Achatzbe343802012-11-11 06:20:58 +0100242 retval = pyra_set_settings(usb_dev, settings);
243 if (retval) {
244 mutex_unlock(&pyra->pyra_lock);
245 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200246 }
Stefan Achatzbe343802012-11-11 06:20:58 +0100247
248 profile_activated(pyra, settings->startup_profile);
249
250 roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
251 roccat_report.value = settings->startup_profile + 1;
252 roccat_report.key = 0;
253 roccat_report_event(pyra->chrdev_minor,
254 (uint8_t const *)&roccat_report);
255
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200256 mutex_unlock(&pyra->pyra_lock);
Stefan Achatzbe343802012-11-11 06:20:58 +0100257 return PYRA_SIZE_SETTINGS;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200258}
259
260
261static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
262 struct device_attribute *attr, char *buf)
263{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000264 struct pyra_device *pyra =
265 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200266 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
267}
268
269static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
270 struct device_attribute *attr, char *buf)
271{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000272 struct pyra_device *pyra =
273 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzbe343802012-11-11 06:20:58 +0100274 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
275 struct pyra_settings settings;
276
277 mutex_lock(&pyra->pyra_lock);
278 roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
279 &settings, PYRA_SIZE_SETTINGS);
280 mutex_unlock(&pyra->pyra_lock);
281
282 return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200283}
284
285static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
Stefan Achatzbe343802012-11-11 06:20:58 +0100288 struct pyra_device *pyra;
289 struct usb_device *usb_dev;
290 struct pyra_info info;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200291
Stefan Achatzbe343802012-11-11 06:20:58 +0100292 dev = dev->parent->parent;
293 pyra = hid_get_drvdata(dev_get_drvdata(dev));
294 usb_dev = interface_to_usbdev(to_usb_interface(dev));
295
296 mutex_lock(&pyra->pyra_lock);
297 roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
298 &info, PYRA_SIZE_INFO);
299 mutex_unlock(&pyra->pyra_lock);
300
301 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200302}
303
Stefan Achatz5012aad2010-11-26 19:57:33 +0000304static struct device_attribute pyra_attributes[] = {
305 __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
306 __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
307 __ATTR(firmware_version, 0440,
308 pyra_sysfs_show_firmware_version, NULL),
309 __ATTR(startup_profile, 0440,
Stefan Achatzbe343802012-11-11 06:20:58 +0100310 pyra_sysfs_show_actual_profile, NULL),
Stefan Achatz5012aad2010-11-26 19:57:33 +0000311 __ATTR_NULL
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200312};
313
Stefan Achatz5012aad2010-11-26 19:57:33 +0000314static struct bin_attribute pyra_bin_attributes[] = {
Stefan Achatzbe343802012-11-11 06:20:58 +0100315 PYRA_BIN_ATTRIBUTE_RW(info, INFO),
316 PYRA_BIN_ATTRIBUTE_W(profile_settings, PROFILE_SETTINGS),
317 PYRA_BIN_ATTRIBUTE_W(profile_buttons, PROFILE_BUTTONS),
318 PYRA_BIN_ATTRIBUTE_RW(settings, SETTINGS),
Stefan Achatz5012aad2010-11-26 19:57:33 +0000319 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200320 .attr = { .name = "profile1_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100321 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000322 .read = pyra_sysfs_read_profilex_settings,
323 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000324 },
325 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200326 .attr = { .name = "profile2_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100327 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000328 .read = pyra_sysfs_read_profilex_settings,
329 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000330 },
331 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200332 .attr = { .name = "profile3_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100333 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000334 .read = pyra_sysfs_read_profilex_settings,
335 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000336 },
337 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200338 .attr = { .name = "profile4_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100339 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000340 .read = pyra_sysfs_read_profilex_settings,
341 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000342 },
343 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200344 .attr = { .name = "profile5_settings", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100345 .size = PYRA_SIZE_PROFILE_SETTINGS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000346 .read = pyra_sysfs_read_profilex_settings,
347 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000348 },
349 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200350 .attr = { .name = "profile1_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100351 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000352 .read = pyra_sysfs_read_profilex_buttons,
353 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000354 },
355 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200356 .attr = { .name = "profile2_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100357 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000358 .read = pyra_sysfs_read_profilex_buttons,
359 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000360 },
361 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200362 .attr = { .name = "profile3_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100363 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000364 .read = pyra_sysfs_read_profilex_buttons,
365 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000366 },
367 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200368 .attr = { .name = "profile4_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100369 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000370 .read = pyra_sysfs_read_profilex_buttons,
371 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000372 },
373 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200374 .attr = { .name = "profile5_buttons", .mode = 0440 },
Stefan Achatzbe343802012-11-11 06:20:58 +0100375 .size = PYRA_SIZE_PROFILE_BUTTONS,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000376 .read = pyra_sysfs_read_profilex_buttons,
377 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000378 },
Stefan Achatz5012aad2010-11-26 19:57:33 +0000379 __ATTR_NULL
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200380};
381
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200382static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
383 struct pyra_device *pyra)
384{
Stefan Achatzbe343802012-11-11 06:20:58 +0100385 struct pyra_settings settings;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200386 int retval, i;
387
388 mutex_init(&pyra->pyra_lock);
389
Stefan Achatzbe343802012-11-11 06:20:58 +0100390 retval = pyra_get_settings(usb_dev, &settings);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200391 if (retval)
392 return retval;
393
394 for (i = 0; i < 5; ++i) {
395 retval = pyra_get_profile_settings(usb_dev,
396 &pyra->profile_settings[i], i);
397 if (retval)
398 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200399 }
400
Stefan Achatzbe343802012-11-11 06:20:58 +0100401 profile_activated(pyra, settings.startup_profile);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200402
403 return 0;
404}
405
406static int pyra_init_specials(struct hid_device *hdev)
407{
408 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
409 struct usb_device *usb_dev = interface_to_usbdev(intf);
410 struct pyra_device *pyra;
411 int retval;
412
413 if (intf->cur_altsetting->desc.bInterfaceProtocol
414 == USB_INTERFACE_PROTOCOL_MOUSE) {
415
416 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
417 if (!pyra) {
Joe Perches4291ee32010-12-09 19:29:03 -0800418 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200419 return -ENOMEM;
420 }
421 hid_set_drvdata(hdev, pyra);
422
423 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
424 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800425 hid_err(hdev, "couldn't init struct pyra_device\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200426 goto exit_free;
427 }
428
Stefan Achatz8211e462011-01-30 13:38:25 +0100429 retval = roccat_connect(pyra_class, hdev,
430 sizeof(struct pyra_roccat_report));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200431 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800432 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200433 } else {
434 pyra->chrdev_minor = retval;
435 pyra->roccat_claimed = 1;
436 }
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200437 } else {
438 hid_set_drvdata(hdev, NULL);
439 }
440
441 return 0;
442exit_free:
443 kfree(pyra);
444 return retval;
445}
446
447static void pyra_remove_specials(struct hid_device *hdev)
448{
449 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
450 struct pyra_device *pyra;
451
452 if (intf->cur_altsetting->desc.bInterfaceProtocol
453 == USB_INTERFACE_PROTOCOL_MOUSE) {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200454 pyra = hid_get_drvdata(hdev);
455 if (pyra->roccat_claimed)
456 roccat_disconnect(pyra->chrdev_minor);
457 kfree(hid_get_drvdata(hdev));
458 }
459}
460
461static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
462{
463 int retval;
464
465 retval = hid_parse(hdev);
466 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800467 hid_err(hdev, "parse failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200468 goto exit;
469 }
470
471 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
472 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800473 hid_err(hdev, "hw start failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200474 goto exit;
475 }
476
477 retval = pyra_init_specials(hdev);
478 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800479 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200480 goto exit_stop;
481 }
482 return 0;
483
484exit_stop:
485 hid_hw_stop(hdev);
486exit:
487 return retval;
488}
489
490static void pyra_remove(struct hid_device *hdev)
491{
492 pyra_remove_specials(hdev);
493 hid_hw_stop(hdev);
494}
495
496static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
497 u8 const *data)
498{
499 struct pyra_mouse_event_button const *button_event;
500
501 switch (data[0]) {
502 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
503 button_event = (struct pyra_mouse_event_button const *)data;
504 switch (button_event->type) {
505 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
506 profile_activated(pyra, button_event->data1 - 1);
507 break;
508 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
509 pyra->actual_cpi = button_event->data1;
510 break;
511 }
512 break;
513 }
514}
515
516static void pyra_report_to_chrdev(struct pyra_device const *pyra,
517 u8 const *data)
518{
519 struct pyra_roccat_report roccat_report;
520 struct pyra_mouse_event_button const *button_event;
521
522 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
523 return;
524
525 button_event = (struct pyra_mouse_event_button const *)data;
526
527 switch (button_event->type) {
528 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
529 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
530 roccat_report.type = button_event->type;
531 roccat_report.value = button_event->data1;
532 roccat_report.key = 0;
533 roccat_report_event(pyra->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100534 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200535 break;
536 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
537 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
538 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
539 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
540 roccat_report.type = button_event->type;
541 roccat_report.key = button_event->data1;
Stefan Achatzd2b570a2010-09-01 12:42:23 +0200542 /*
543 * pyra reports profile numbers with range 1-5.
544 * Keeping this behaviour.
545 */
546 roccat_report.value = pyra->actual_profile + 1;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200547 roccat_report_event(pyra->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100548 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200549 }
550 break;
551 }
552}
553
554static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
555 u8 *data, int size)
556{
557 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
558 struct pyra_device *pyra = hid_get_drvdata(hdev);
559
560 if (intf->cur_altsetting->desc.bInterfaceProtocol
561 != USB_INTERFACE_PROTOCOL_MOUSE)
562 return 0;
563
Stefan Achatz901e64d2011-06-12 10:02:44 +0200564 if (pyra == NULL)
565 return 0;
566
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200567 pyra_keep_values_up_to_date(pyra, data);
568
569 if (pyra->roccat_claimed)
570 pyra_report_to_chrdev(pyra, data);
571
572 return 0;
573}
574
575static const struct hid_device_id pyra_devices[] = {
576 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
577 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
Stefan Achatz3fce2242011-03-23 18:11:36 +0100578 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
579 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200580 { }
581};
582
583MODULE_DEVICE_TABLE(hid, pyra_devices);
584
585static struct hid_driver pyra_driver = {
586 .name = "pyra",
587 .id_table = pyra_devices,
588 .probe = pyra_probe,
589 .remove = pyra_remove,
590 .raw_event = pyra_raw_event
591};
592
593static int __init pyra_init(void)
594{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000595 int retval;
596
597 /* class name has to be same as driver name */
598 pyra_class = class_create(THIS_MODULE, "pyra");
599 if (IS_ERR(pyra_class))
600 return PTR_ERR(pyra_class);
601 pyra_class->dev_attrs = pyra_attributes;
602 pyra_class->dev_bin_attrs = pyra_bin_attributes;
603
604 retval = hid_register_driver(&pyra_driver);
605 if (retval)
606 class_destroy(pyra_class);
607 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200608}
609
610static void __exit pyra_exit(void)
611{
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200612 hid_unregister_driver(&pyra_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100613 class_destroy(pyra_class);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200614}
615
616module_init(pyra_init);
617module_exit(pyra_exit);
618
619MODULE_AUTHOR("Stefan Achatz");
620MODULE_DESCRIPTION("USB Roccat Pyra driver");
621MODULE_LICENSE("GPL v2");