blob: 8140776bd8c51906f7f1296ef1d1ea5ff6d49299 [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 Achatzcb7cf3d2010-08-29 12:30:18 +020045 struct pyra_control control;
46
47 if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
48 request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
49 (value < 0 || value > 4))
50 return -EINVAL;
51
52 control.command = PYRA_COMMAND_CONTROL;
53 control.value = value;
54 control.request = request;
55
Stefan Achatz1edd5b42011-06-01 15:54:17 +020056 return roccat_common_send(usb_dev, PYRA_COMMAND_CONTROL,
Stefan Achatz5772f632011-01-30 13:38:23 +010057 &control, sizeof(struct pyra_control));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020058}
59
60static int pyra_receive_control_status(struct usb_device *usb_dev)
61{
Stefan Achatz5772f632011-01-30 13:38:23 +010062 int retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020063 struct pyra_control control;
64
65 do {
66 msleep(10);
Stefan Achatz1edd5b42011-06-01 15:54:17 +020067 retval = roccat_common_receive(usb_dev, PYRA_COMMAND_CONTROL,
Stefan Achatz5772f632011-01-30 13:38:23 +010068 &control, sizeof(struct pyra_control));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020069
70 /* requested too early, try again */
Stefan Achatz5772f632011-01-30 13:38:23 +010071 } while (retval == -EPROTO);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020072
Stefan Achatz5772f632011-01-30 13:38:23 +010073 if (!retval && control.command == PYRA_COMMAND_CONTROL &&
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020074 control.request == PYRA_CONTROL_REQUEST_STATUS &&
75 control.value == 1)
Stefan Achatz5772f632011-01-30 13:38:23 +010076 return 0;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020077 else {
Joe Perches4291ee32010-12-09 19:29:03 -080078 hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n",
79 control.request, control.value);
Stefan Achatz5772f632011-01-30 13:38:23 +010080 return retval ? retval : -EINVAL;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020081 }
82}
83
84static int pyra_get_profile_settings(struct usb_device *usb_dev,
85 struct pyra_profile_settings *buf, int number)
86{
87 int retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020088 retval = pyra_send_control(usb_dev, number,
89 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020090 if (retval)
91 return retval;
Stefan Achatz1edd5b42011-06-01 15:54:17 +020092 return roccat_common_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS,
Stefan Achatz5772f632011-01-30 13:38:23 +010093 buf, sizeof(struct pyra_profile_settings));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +020094}
95
96static int pyra_get_profile_buttons(struct usb_device *usb_dev,
97 struct pyra_profile_buttons *buf, int number)
98{
99 int retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200100 retval = pyra_send_control(usb_dev, number,
101 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200102 if (retval)
103 return retval;
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200104 return roccat_common_receive(usb_dev, PYRA_COMMAND_PROFILE_BUTTONS,
Stefan Achatz5772f632011-01-30 13:38:23 +0100105 buf, sizeof(struct pyra_profile_buttons));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200106}
107
108static int pyra_get_settings(struct usb_device *usb_dev,
109 struct pyra_settings *buf)
110{
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200111 return roccat_common_receive(usb_dev, PYRA_COMMAND_SETTINGS,
Stefan Achatz5772f632011-01-30 13:38:23 +0100112 buf, sizeof(struct pyra_settings));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200113}
114
115static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf)
116{
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200117 return roccat_common_receive(usb_dev, PYRA_COMMAND_INFO,
Stefan Achatz5772f632011-01-30 13:38:23 +0100118 buf, sizeof(struct pyra_info));
119}
120
121static int pyra_send(struct usb_device *usb_dev, uint command,
122 void const *buf, uint size)
123{
124 int retval;
125 retval = roccat_common_send(usb_dev, command, buf, size);
126 if (retval)
127 return retval;
128 return pyra_receive_control_status(usb_dev);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200129}
130
131static int pyra_set_profile_settings(struct usb_device *usb_dev,
132 struct pyra_profile_settings const *settings)
133{
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200134 return pyra_send(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS, settings,
Stefan Achatz5772f632011-01-30 13:38:23 +0100135 sizeof(struct pyra_profile_settings));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200136}
137
138static int pyra_set_profile_buttons(struct usb_device *usb_dev,
139 struct pyra_profile_buttons const *buttons)
140{
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200141 return pyra_send(usb_dev, PYRA_COMMAND_PROFILE_BUTTONS, buttons,
Stefan Achatz5772f632011-01-30 13:38:23 +0100142 sizeof(struct pyra_profile_buttons));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200143}
144
145static int pyra_set_settings(struct usb_device *usb_dev,
146 struct pyra_settings const *settings)
147{
Stefan Achatz1edd5b42011-06-01 15:54:17 +0200148 return pyra_send(usb_dev, PYRA_COMMAND_SETTINGS, settings,
Stefan Achatz5772f632011-01-30 13:38:23 +0100149 sizeof(struct pyra_settings));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200150}
151
152static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
153 struct kobject *kobj, struct bin_attribute *attr, char *buf,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000154 loff_t off, size_t count)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200155{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000156 struct device *dev =
157 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200158 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
159
160 if (off >= sizeof(struct pyra_profile_settings))
161 return 0;
162
163 if (off + count > sizeof(struct pyra_profile_settings))
164 count = sizeof(struct pyra_profile_settings) - off;
165
166 mutex_lock(&pyra->pyra_lock);
Stefan Achatz14a057f2010-11-26 19:57:38 +0000167 memcpy(buf, ((char const *)&pyra->profile_settings[*(uint *)(attr->private)]) + off,
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200168 count);
169 mutex_unlock(&pyra->pyra_lock);
170
171 return count;
172}
173
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200174static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
175 struct kobject *kobj, struct bin_attribute *attr, char *buf,
Stefan Achatz14a057f2010-11-26 19:57:38 +0000176 loff_t off, size_t count)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200177{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000178 struct device *dev =
179 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200180 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
181
182 if (off >= sizeof(struct pyra_profile_buttons))
183 return 0;
184
185 if (off + count > sizeof(struct pyra_profile_buttons))
186 count = sizeof(struct pyra_profile_buttons) - off;
187
188 mutex_lock(&pyra->pyra_lock);
Stefan Achatz14a057f2010-11-26 19:57:38 +0000189 memcpy(buf, ((char const *)&pyra->profile_buttons[*(uint *)(attr->private)]) + off,
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200190 count);
191 mutex_unlock(&pyra->pyra_lock);
192
193 return count;
194}
195
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200196static ssize_t pyra_sysfs_write_profile_settings(struct file *fp,
197 struct kobject *kobj, struct bin_attribute *attr, char *buf,
198 loff_t off, size_t count)
199{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000200 struct device *dev =
201 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200202 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
203 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
204 int retval = 0;
205 int difference;
206 int profile_number;
207 struct pyra_profile_settings *profile_settings;
208
209 if (off != 0 || count != sizeof(struct pyra_profile_settings))
210 return -EINVAL;
211
212 profile_number = ((struct pyra_profile_settings const *)buf)->number;
213 profile_settings = &pyra->profile_settings[profile_number];
214
215 mutex_lock(&pyra->pyra_lock);
216 difference = memcmp(buf, profile_settings,
217 sizeof(struct pyra_profile_settings));
218 if (difference) {
219 retval = pyra_set_profile_settings(usb_dev,
220 (struct pyra_profile_settings const *)buf);
221 if (!retval)
222 memcpy(profile_settings, buf,
223 sizeof(struct pyra_profile_settings));
224 }
225 mutex_unlock(&pyra->pyra_lock);
226
227 if (retval)
228 return retval;
229
230 return sizeof(struct pyra_profile_settings);
231}
232
233static ssize_t pyra_sysfs_write_profile_buttons(struct file *fp,
234 struct kobject *kobj, struct bin_attribute *attr, char *buf,
235 loff_t off, size_t count)
236{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000237 struct device *dev =
238 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200239 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
240 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
241 int retval = 0;
242 int difference;
243 int profile_number;
244 struct pyra_profile_buttons *profile_buttons;
245
246 if (off != 0 || count != sizeof(struct pyra_profile_buttons))
247 return -EINVAL;
248
249 profile_number = ((struct pyra_profile_buttons const *)buf)->number;
250 profile_buttons = &pyra->profile_buttons[profile_number];
251
252 mutex_lock(&pyra->pyra_lock);
253 difference = memcmp(buf, profile_buttons,
254 sizeof(struct pyra_profile_buttons));
255 if (difference) {
256 retval = pyra_set_profile_buttons(usb_dev,
257 (struct pyra_profile_buttons const *)buf);
258 if (!retval)
259 memcpy(profile_buttons, buf,
260 sizeof(struct pyra_profile_buttons));
261 }
262 mutex_unlock(&pyra->pyra_lock);
263
264 if (retval)
265 return retval;
266
267 return sizeof(struct pyra_profile_buttons);
268}
269
270static ssize_t pyra_sysfs_read_settings(struct file *fp,
271 struct kobject *kobj, struct bin_attribute *attr, char *buf,
272 loff_t off, size_t count)
273{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000274 struct device *dev =
275 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200276 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
277
278 if (off >= sizeof(struct pyra_settings))
279 return 0;
280
281 if (off + count > sizeof(struct pyra_settings))
282 count = sizeof(struct pyra_settings) - off;
283
284 mutex_lock(&pyra->pyra_lock);
285 memcpy(buf, ((char const *)&pyra->settings) + off, count);
286 mutex_unlock(&pyra->pyra_lock);
287
288 return count;
289}
290
291static ssize_t pyra_sysfs_write_settings(struct file *fp,
292 struct kobject *kobj, struct bin_attribute *attr, char *buf,
293 loff_t off, size_t count)
294{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000295 struct device *dev =
296 container_of(kobj, struct device, kobj)->parent->parent;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200297 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
298 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
299 int retval = 0;
300 int difference;
301
302 if (off != 0 || count != sizeof(struct pyra_settings))
303 return -EINVAL;
304
305 mutex_lock(&pyra->pyra_lock);
306 difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings));
307 if (difference) {
308 retval = pyra_set_settings(usb_dev,
309 (struct pyra_settings const *)buf);
310 if (!retval)
311 memcpy(&pyra->settings, buf,
312 sizeof(struct pyra_settings));
313 }
314 mutex_unlock(&pyra->pyra_lock);
315
316 if (retval)
317 return retval;
318
319 profile_activated(pyra, pyra->settings.startup_profile);
320
321 return sizeof(struct pyra_settings);
322}
323
324
325static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
326 struct device_attribute *attr, char *buf)
327{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000328 struct pyra_device *pyra =
329 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200330 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
331}
332
333static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
334 struct device_attribute *attr, char *buf)
335{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000336 struct pyra_device *pyra =
337 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200338 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile);
339}
340
341static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
342 struct device_attribute *attr, char *buf)
343{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000344 struct pyra_device *pyra =
345 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200346 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version);
347}
348
349static ssize_t pyra_sysfs_show_startup_profile(struct device *dev,
350 struct device_attribute *attr, char *buf)
351{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000352 struct pyra_device *pyra =
353 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200354 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile);
355}
356
Stefan Achatz5012aad2010-11-26 19:57:33 +0000357static struct device_attribute pyra_attributes[] = {
358 __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
359 __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
360 __ATTR(firmware_version, 0440,
361 pyra_sysfs_show_firmware_version, NULL),
362 __ATTR(startup_profile, 0440,
363 pyra_sysfs_show_startup_profile, NULL),
364 __ATTR_NULL
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200365};
366
Stefan Achatz5012aad2010-11-26 19:57:33 +0000367static struct bin_attribute pyra_bin_attributes[] = {
368 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200369 .attr = { .name = "profile_settings", .mode = 0220 },
370 .size = sizeof(struct pyra_profile_settings),
371 .write = pyra_sysfs_write_profile_settings
Stefan Achatz5012aad2010-11-26 19:57:33 +0000372 },
373 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200374 .attr = { .name = "profile1_settings", .mode = 0440 },
375 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000376 .read = pyra_sysfs_read_profilex_settings,
377 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000378 },
379 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200380 .attr = { .name = "profile2_settings", .mode = 0440 },
381 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000382 .read = pyra_sysfs_read_profilex_settings,
383 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000384 },
385 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200386 .attr = { .name = "profile3_settings", .mode = 0440 },
387 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000388 .read = pyra_sysfs_read_profilex_settings,
389 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000390 },
391 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200392 .attr = { .name = "profile4_settings", .mode = 0440 },
393 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000394 .read = pyra_sysfs_read_profilex_settings,
395 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000396 },
397 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200398 .attr = { .name = "profile5_settings", .mode = 0440 },
399 .size = sizeof(struct pyra_profile_settings),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000400 .read = pyra_sysfs_read_profilex_settings,
401 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000402 },
403 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200404 .attr = { .name = "profile_buttons", .mode = 0220 },
405 .size = sizeof(struct pyra_profile_buttons),
406 .write = pyra_sysfs_write_profile_buttons
Stefan Achatz5012aad2010-11-26 19:57:33 +0000407 },
408 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200409 .attr = { .name = "profile1_buttons", .mode = 0440 },
410 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000411 .read = pyra_sysfs_read_profilex_buttons,
412 .private = &profile_numbers[0]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000413 },
414 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200415 .attr = { .name = "profile2_buttons", .mode = 0440 },
416 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000417 .read = pyra_sysfs_read_profilex_buttons,
418 .private = &profile_numbers[1]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000419 },
420 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200421 .attr = { .name = "profile3_buttons", .mode = 0440 },
422 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000423 .read = pyra_sysfs_read_profilex_buttons,
424 .private = &profile_numbers[2]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000425 },
426 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200427 .attr = { .name = "profile4_buttons", .mode = 0440 },
428 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000429 .read = pyra_sysfs_read_profilex_buttons,
430 .private = &profile_numbers[3]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000431 },
432 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200433 .attr = { .name = "profile5_buttons", .mode = 0440 },
434 .size = sizeof(struct pyra_profile_buttons),
Stefan Achatz14a057f2010-11-26 19:57:38 +0000435 .read = pyra_sysfs_read_profilex_buttons,
436 .private = &profile_numbers[4]
Stefan Achatz5012aad2010-11-26 19:57:33 +0000437 },
438 {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200439 .attr = { .name = "settings", .mode = 0660 },
440 .size = sizeof(struct pyra_settings),
441 .read = pyra_sysfs_read_settings,
442 .write = pyra_sysfs_write_settings
Stefan Achatz5012aad2010-11-26 19:57:33 +0000443 },
444 __ATTR_NULL
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200445};
446
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200447static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
448 struct pyra_device *pyra)
449{
Stefan Achatz5772f632011-01-30 13:38:23 +0100450 struct pyra_info info;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200451 int retval, i;
452
453 mutex_init(&pyra->pyra_lock);
454
Stefan Achatz5772f632011-01-30 13:38:23 +0100455 retval = pyra_get_info(usb_dev, &info);
456 if (retval)
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200457 return retval;
Stefan Achatz5772f632011-01-30 13:38:23 +0100458
459 pyra->firmware_version = info.firmware_version;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200460
461 retval = pyra_get_settings(usb_dev, &pyra->settings);
462 if (retval)
463 return retval;
464
465 for (i = 0; i < 5; ++i) {
466 retval = pyra_get_profile_settings(usb_dev,
467 &pyra->profile_settings[i], i);
468 if (retval)
469 return retval;
470
471 retval = pyra_get_profile_buttons(usb_dev,
472 &pyra->profile_buttons[i], i);
473 if (retval)
474 return retval;
475 }
476
477 profile_activated(pyra, pyra->settings.startup_profile);
478
479 return 0;
480}
481
482static int pyra_init_specials(struct hid_device *hdev)
483{
484 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
485 struct usb_device *usb_dev = interface_to_usbdev(intf);
486 struct pyra_device *pyra;
487 int retval;
488
489 if (intf->cur_altsetting->desc.bInterfaceProtocol
490 == USB_INTERFACE_PROTOCOL_MOUSE) {
491
492 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
493 if (!pyra) {
Joe Perches4291ee32010-12-09 19:29:03 -0800494 hid_err(hdev, "can't alloc device descriptor\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200495 return -ENOMEM;
496 }
497 hid_set_drvdata(hdev, pyra);
498
499 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
500 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800501 hid_err(hdev, "couldn't init struct pyra_device\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200502 goto exit_free;
503 }
504
Stefan Achatz8211e462011-01-30 13:38:25 +0100505 retval = roccat_connect(pyra_class, hdev,
506 sizeof(struct pyra_roccat_report));
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200507 if (retval < 0) {
Joe Perches4291ee32010-12-09 19:29:03 -0800508 hid_err(hdev, "couldn't init char dev\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200509 } else {
510 pyra->chrdev_minor = retval;
511 pyra->roccat_claimed = 1;
512 }
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200513 } else {
514 hid_set_drvdata(hdev, NULL);
515 }
516
517 return 0;
518exit_free:
519 kfree(pyra);
520 return retval;
521}
522
523static void pyra_remove_specials(struct hid_device *hdev)
524{
525 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
526 struct pyra_device *pyra;
527
528 if (intf->cur_altsetting->desc.bInterfaceProtocol
529 == USB_INTERFACE_PROTOCOL_MOUSE) {
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200530 pyra = hid_get_drvdata(hdev);
531 if (pyra->roccat_claimed)
532 roccat_disconnect(pyra->chrdev_minor);
533 kfree(hid_get_drvdata(hdev));
534 }
535}
536
537static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
538{
539 int retval;
540
541 retval = hid_parse(hdev);
542 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800543 hid_err(hdev, "parse failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200544 goto exit;
545 }
546
547 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
548 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800549 hid_err(hdev, "hw start failed\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200550 goto exit;
551 }
552
553 retval = pyra_init_specials(hdev);
554 if (retval) {
Joe Perches4291ee32010-12-09 19:29:03 -0800555 hid_err(hdev, "couldn't install mouse\n");
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200556 goto exit_stop;
557 }
558 return 0;
559
560exit_stop:
561 hid_hw_stop(hdev);
562exit:
563 return retval;
564}
565
566static void pyra_remove(struct hid_device *hdev)
567{
568 pyra_remove_specials(hdev);
569 hid_hw_stop(hdev);
570}
571
572static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
573 u8 const *data)
574{
575 struct pyra_mouse_event_button const *button_event;
576
577 switch (data[0]) {
578 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
579 button_event = (struct pyra_mouse_event_button const *)data;
580 switch (button_event->type) {
581 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
582 profile_activated(pyra, button_event->data1 - 1);
583 break;
584 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
585 pyra->actual_cpi = button_event->data1;
586 break;
587 }
588 break;
589 }
590}
591
592static void pyra_report_to_chrdev(struct pyra_device const *pyra,
593 u8 const *data)
594{
595 struct pyra_roccat_report roccat_report;
596 struct pyra_mouse_event_button const *button_event;
597
598 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
599 return;
600
601 button_event = (struct pyra_mouse_event_button const *)data;
602
603 switch (button_event->type) {
604 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
605 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
606 roccat_report.type = button_event->type;
607 roccat_report.value = button_event->data1;
608 roccat_report.key = 0;
609 roccat_report_event(pyra->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100610 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200611 break;
612 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
613 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
614 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
615 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
616 roccat_report.type = button_event->type;
617 roccat_report.key = button_event->data1;
Stefan Achatzd2b570a2010-09-01 12:42:23 +0200618 /*
619 * pyra reports profile numbers with range 1-5.
620 * Keeping this behaviour.
621 */
622 roccat_report.value = pyra->actual_profile + 1;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200623 roccat_report_event(pyra->chrdev_minor,
Stefan Achatz8211e462011-01-30 13:38:25 +0100624 (uint8_t const *)&roccat_report);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200625 }
626 break;
627 }
628}
629
630static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
631 u8 *data, int size)
632{
633 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
634 struct pyra_device *pyra = hid_get_drvdata(hdev);
635
636 if (intf->cur_altsetting->desc.bInterfaceProtocol
637 != USB_INTERFACE_PROTOCOL_MOUSE)
638 return 0;
639
Stefan Achatz901e64d2011-06-12 10:02:44 +0200640 if (pyra == NULL)
641 return 0;
642
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200643 pyra_keep_values_up_to_date(pyra, data);
644
645 if (pyra->roccat_claimed)
646 pyra_report_to_chrdev(pyra, data);
647
648 return 0;
649}
650
651static const struct hid_device_id pyra_devices[] = {
652 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
653 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
Stefan Achatz3fce2242011-03-23 18:11:36 +0100654 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
655 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200656 { }
657};
658
659MODULE_DEVICE_TABLE(hid, pyra_devices);
660
661static struct hid_driver pyra_driver = {
662 .name = "pyra",
663 .id_table = pyra_devices,
664 .probe = pyra_probe,
665 .remove = pyra_remove,
666 .raw_event = pyra_raw_event
667};
668
669static int __init pyra_init(void)
670{
Stefan Achatz5012aad2010-11-26 19:57:33 +0000671 int retval;
672
673 /* class name has to be same as driver name */
674 pyra_class = class_create(THIS_MODULE, "pyra");
675 if (IS_ERR(pyra_class))
676 return PTR_ERR(pyra_class);
677 pyra_class->dev_attrs = pyra_attributes;
678 pyra_class->dev_bin_attrs = pyra_bin_attributes;
679
680 retval = hid_register_driver(&pyra_driver);
681 if (retval)
682 class_destroy(pyra_class);
683 return retval;
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200684}
685
686static void __exit pyra_exit(void)
687{
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200688 hid_unregister_driver(&pyra_driver);
Stefan Achatz74b643d2011-01-30 13:38:27 +0100689 class_destroy(pyra_class);
Stefan Achatzcb7cf3d2010-08-29 12:30:18 +0200690}
691
692module_init(pyra_init);
693module_exit(pyra_exit);
694
695MODULE_AUTHOR("Stefan Achatz");
696MODULE_DESCRIPTION("USB Roccat Pyra driver");
697MODULE_LICENSE("GPL v2");