2 * Roccat Pyra driver for Linux
4 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
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)
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
20 #include <linux/device.h>
21 #include <linux/input.h>
22 #include <linux/hid.h>
23 #include <linux/module.h>
24 #include <linux/slab.h>
25 #include <linux/hid-roccat.h>
27 #include "hid-roccat-common.h"
28 #include "hid-roccat-pyra.h"
30 static uint profile_numbers
[5] = {0, 1, 2, 3, 4};
32 /* pyra_class is used for creating sysfs attributes via roccat char device */
33 static struct class *pyra_class
;
35 static void profile_activated(struct pyra_device
*pyra
,
36 unsigned int new_profile
)
38 pyra
->actual_profile
= new_profile
;
39 pyra
->actual_cpi
= pyra
->profile_settings
[pyra
->actual_profile
].y_cpi
;
42 static int pyra_send_control(struct usb_device
*usb_dev
, int value
,
43 enum pyra_control_requests request
)
45 struct pyra_control control
;
47 if ((request
== PYRA_CONTROL_REQUEST_PROFILE_SETTINGS
||
48 request
== PYRA_CONTROL_REQUEST_PROFILE_BUTTONS
) &&
49 (value
< 0 || value
> 4))
52 control
.command
= PYRA_COMMAND_CONTROL
;
53 control
.value
= value
;
54 control
.request
= request
;
56 return roccat_common_send(usb_dev
, PYRA_USB_COMMAND_CONTROL
,
57 &control
, sizeof(struct pyra_control
));
60 static int pyra_receive_control_status(struct usb_device
*usb_dev
)
63 struct pyra_control control
;
67 retval
= roccat_common_receive(usb_dev
, PYRA_USB_COMMAND_CONTROL
,
68 &control
, sizeof(struct pyra_control
));
70 /* requested too early, try again */
71 } while (retval
== -EPROTO
);
73 if (!retval
&& control
.command
== PYRA_COMMAND_CONTROL
&&
74 control
.request
== PYRA_CONTROL_REQUEST_STATUS
&&
78 hid_err(usb_dev
, "receive control status: unknown response 0x%x 0x%x\n",
79 control
.request
, control
.value
);
80 return retval
? retval
: -EINVAL
;
84 static int pyra_get_profile_settings(struct usb_device
*usb_dev
,
85 struct pyra_profile_settings
*buf
, int number
)
88 retval
= pyra_send_control(usb_dev
, number
,
89 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS
);
92 return roccat_common_receive(usb_dev
, PYRA_USB_COMMAND_PROFILE_SETTINGS
,
93 buf
, sizeof(struct pyra_profile_settings
));
96 static int pyra_get_profile_buttons(struct usb_device
*usb_dev
,
97 struct pyra_profile_buttons
*buf
, int number
)
100 retval
= pyra_send_control(usb_dev
, number
,
101 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS
);
104 return roccat_common_receive(usb_dev
, PYRA_USB_COMMAND_PROFILE_BUTTONS
,
105 buf
, sizeof(struct pyra_profile_buttons
));
108 static int pyra_get_settings(struct usb_device
*usb_dev
,
109 struct pyra_settings
*buf
)
111 return roccat_common_receive(usb_dev
, PYRA_USB_COMMAND_SETTINGS
,
112 buf
, sizeof(struct pyra_settings
));
115 static int pyra_get_info(struct usb_device
*usb_dev
, struct pyra_info
*buf
)
117 return roccat_common_receive(usb_dev
, PYRA_USB_COMMAND_INFO
,
118 buf
, sizeof(struct pyra_info
));
121 static int pyra_send(struct usb_device
*usb_dev
, uint command
,
122 void const *buf
, uint size
)
125 retval
= roccat_common_send(usb_dev
, command
, buf
, size
);
128 return pyra_receive_control_status(usb_dev
);
131 static int pyra_set_profile_settings(struct usb_device
*usb_dev
,
132 struct pyra_profile_settings
const *settings
)
134 return pyra_send(usb_dev
, PYRA_USB_COMMAND_PROFILE_SETTINGS
, settings
,
135 sizeof(struct pyra_profile_settings
));
138 static int pyra_set_profile_buttons(struct usb_device
*usb_dev
,
139 struct pyra_profile_buttons
const *buttons
)
141 return pyra_send(usb_dev
, PYRA_USB_COMMAND_PROFILE_BUTTONS
, buttons
,
142 sizeof(struct pyra_profile_buttons
));
145 static int pyra_set_settings(struct usb_device
*usb_dev
,
146 struct pyra_settings
const *settings
)
149 retval
= roccat_common_send(usb_dev
, PYRA_USB_COMMAND_SETTINGS
, settings
,
150 sizeof(struct pyra_settings
));
153 return pyra_receive_control_status(usb_dev
);
156 static ssize_t
pyra_sysfs_read_profilex_settings(struct file
*fp
,
157 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
158 loff_t off
, size_t count
)
161 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
162 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
164 if (off
>= sizeof(struct pyra_profile_settings
))
167 if (off
+ count
> sizeof(struct pyra_profile_settings
))
168 count
= sizeof(struct pyra_profile_settings
) - off
;
170 mutex_lock(&pyra
->pyra_lock
);
171 memcpy(buf
, ((char const *)&pyra
->profile_settings
[*(uint
*)(attr
->private)]) + off
,
173 mutex_unlock(&pyra
->pyra_lock
);
178 static ssize_t
pyra_sysfs_read_profilex_buttons(struct file
*fp
,
179 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
180 loff_t off
, size_t count
)
183 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
184 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
186 if (off
>= sizeof(struct pyra_profile_buttons
))
189 if (off
+ count
> sizeof(struct pyra_profile_buttons
))
190 count
= sizeof(struct pyra_profile_buttons
) - off
;
192 mutex_lock(&pyra
->pyra_lock
);
193 memcpy(buf
, ((char const *)&pyra
->profile_buttons
[*(uint
*)(attr
->private)]) + off
,
195 mutex_unlock(&pyra
->pyra_lock
);
200 static ssize_t
pyra_sysfs_write_profile_settings(struct file
*fp
,
201 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
202 loff_t off
, size_t count
)
205 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
206 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
207 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
211 struct pyra_profile_settings
*profile_settings
;
213 if (off
!= 0 || count
!= sizeof(struct pyra_profile_settings
))
216 profile_number
= ((struct pyra_profile_settings
const *)buf
)->number
;
217 profile_settings
= &pyra
->profile_settings
[profile_number
];
219 mutex_lock(&pyra
->pyra_lock
);
220 difference
= memcmp(buf
, profile_settings
,
221 sizeof(struct pyra_profile_settings
));
223 retval
= pyra_set_profile_settings(usb_dev
,
224 (struct pyra_profile_settings
const *)buf
);
226 memcpy(profile_settings
, buf
,
227 sizeof(struct pyra_profile_settings
));
229 mutex_unlock(&pyra
->pyra_lock
);
234 return sizeof(struct pyra_profile_settings
);
237 static ssize_t
pyra_sysfs_write_profile_buttons(struct file
*fp
,
238 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
239 loff_t off
, size_t count
)
242 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
243 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
244 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
248 struct pyra_profile_buttons
*profile_buttons
;
250 if (off
!= 0 || count
!= sizeof(struct pyra_profile_buttons
))
253 profile_number
= ((struct pyra_profile_buttons
const *)buf
)->number
;
254 profile_buttons
= &pyra
->profile_buttons
[profile_number
];
256 mutex_lock(&pyra
->pyra_lock
);
257 difference
= memcmp(buf
, profile_buttons
,
258 sizeof(struct pyra_profile_buttons
));
260 retval
= pyra_set_profile_buttons(usb_dev
,
261 (struct pyra_profile_buttons
const *)buf
);
263 memcpy(profile_buttons
, buf
,
264 sizeof(struct pyra_profile_buttons
));
266 mutex_unlock(&pyra
->pyra_lock
);
271 return sizeof(struct pyra_profile_buttons
);
274 static ssize_t
pyra_sysfs_read_settings(struct file
*fp
,
275 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
276 loff_t off
, size_t count
)
279 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
280 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
282 if (off
>= sizeof(struct pyra_settings
))
285 if (off
+ count
> sizeof(struct pyra_settings
))
286 count
= sizeof(struct pyra_settings
) - off
;
288 mutex_lock(&pyra
->pyra_lock
);
289 memcpy(buf
, ((char const *)&pyra
->settings
) + off
, count
);
290 mutex_unlock(&pyra
->pyra_lock
);
295 static ssize_t
pyra_sysfs_write_settings(struct file
*fp
,
296 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
297 loff_t off
, size_t count
)
300 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
301 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
302 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
306 if (off
!= 0 || count
!= sizeof(struct pyra_settings
))
309 mutex_lock(&pyra
->pyra_lock
);
310 difference
= memcmp(buf
, &pyra
->settings
, sizeof(struct pyra_settings
));
312 retval
= pyra_set_settings(usb_dev
,
313 (struct pyra_settings
const *)buf
);
315 memcpy(&pyra
->settings
, buf
,
316 sizeof(struct pyra_settings
));
318 mutex_unlock(&pyra
->pyra_lock
);
323 profile_activated(pyra
, pyra
->settings
.startup_profile
);
325 return sizeof(struct pyra_settings
);
329 static ssize_t
pyra_sysfs_show_actual_cpi(struct device
*dev
,
330 struct device_attribute
*attr
, char *buf
)
332 struct pyra_device
*pyra
=
333 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
334 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->actual_cpi
);
337 static ssize_t
pyra_sysfs_show_actual_profile(struct device
*dev
,
338 struct device_attribute
*attr
, char *buf
)
340 struct pyra_device
*pyra
=
341 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
342 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->actual_profile
);
345 static ssize_t
pyra_sysfs_show_firmware_version(struct device
*dev
,
346 struct device_attribute
*attr
, char *buf
)
348 struct pyra_device
*pyra
=
349 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
350 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->firmware_version
);
353 static ssize_t
pyra_sysfs_show_startup_profile(struct device
*dev
,
354 struct device_attribute
*attr
, char *buf
)
356 struct pyra_device
*pyra
=
357 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
358 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->settings
.startup_profile
);
361 static struct device_attribute pyra_attributes
[] = {
362 __ATTR(actual_cpi
, 0440, pyra_sysfs_show_actual_cpi
, NULL
),
363 __ATTR(actual_profile
, 0440, pyra_sysfs_show_actual_profile
, NULL
),
364 __ATTR(firmware_version
, 0440,
365 pyra_sysfs_show_firmware_version
, NULL
),
366 __ATTR(startup_profile
, 0440,
367 pyra_sysfs_show_startup_profile
, NULL
),
371 static struct bin_attribute pyra_bin_attributes
[] = {
373 .attr
= { .name
= "profile_settings", .mode
= 0220 },
374 .size
= sizeof(struct pyra_profile_settings
),
375 .write
= pyra_sysfs_write_profile_settings
378 .attr
= { .name
= "profile1_settings", .mode
= 0440 },
379 .size
= sizeof(struct pyra_profile_settings
),
380 .read
= pyra_sysfs_read_profilex_settings
,
381 .private = &profile_numbers
[0]
384 .attr
= { .name
= "profile2_settings", .mode
= 0440 },
385 .size
= sizeof(struct pyra_profile_settings
),
386 .read
= pyra_sysfs_read_profilex_settings
,
387 .private = &profile_numbers
[1]
390 .attr
= { .name
= "profile3_settings", .mode
= 0440 },
391 .size
= sizeof(struct pyra_profile_settings
),
392 .read
= pyra_sysfs_read_profilex_settings
,
393 .private = &profile_numbers
[2]
396 .attr
= { .name
= "profile4_settings", .mode
= 0440 },
397 .size
= sizeof(struct pyra_profile_settings
),
398 .read
= pyra_sysfs_read_profilex_settings
,
399 .private = &profile_numbers
[3]
402 .attr
= { .name
= "profile5_settings", .mode
= 0440 },
403 .size
= sizeof(struct pyra_profile_settings
),
404 .read
= pyra_sysfs_read_profilex_settings
,
405 .private = &profile_numbers
[4]
408 .attr
= { .name
= "profile_buttons", .mode
= 0220 },
409 .size
= sizeof(struct pyra_profile_buttons
),
410 .write
= pyra_sysfs_write_profile_buttons
413 .attr
= { .name
= "profile1_buttons", .mode
= 0440 },
414 .size
= sizeof(struct pyra_profile_buttons
),
415 .read
= pyra_sysfs_read_profilex_buttons
,
416 .private = &profile_numbers
[0]
419 .attr
= { .name
= "profile2_buttons", .mode
= 0440 },
420 .size
= sizeof(struct pyra_profile_buttons
),
421 .read
= pyra_sysfs_read_profilex_buttons
,
422 .private = &profile_numbers
[1]
425 .attr
= { .name
= "profile3_buttons", .mode
= 0440 },
426 .size
= sizeof(struct pyra_profile_buttons
),
427 .read
= pyra_sysfs_read_profilex_buttons
,
428 .private = &profile_numbers
[2]
431 .attr
= { .name
= "profile4_buttons", .mode
= 0440 },
432 .size
= sizeof(struct pyra_profile_buttons
),
433 .read
= pyra_sysfs_read_profilex_buttons
,
434 .private = &profile_numbers
[3]
437 .attr
= { .name
= "profile5_buttons", .mode
= 0440 },
438 .size
= sizeof(struct pyra_profile_buttons
),
439 .read
= pyra_sysfs_read_profilex_buttons
,
440 .private = &profile_numbers
[4]
443 .attr
= { .name
= "settings", .mode
= 0660 },
444 .size
= sizeof(struct pyra_settings
),
445 .read
= pyra_sysfs_read_settings
,
446 .write
= pyra_sysfs_write_settings
451 static int pyra_init_pyra_device_struct(struct usb_device
*usb_dev
,
452 struct pyra_device
*pyra
)
454 struct pyra_info info
;
457 mutex_init(&pyra
->pyra_lock
);
459 retval
= pyra_get_info(usb_dev
, &info
);
463 pyra
->firmware_version
= info
.firmware_version
;
465 retval
= pyra_get_settings(usb_dev
, &pyra
->settings
);
469 for (i
= 0; i
< 5; ++i
) {
470 retval
= pyra_get_profile_settings(usb_dev
,
471 &pyra
->profile_settings
[i
], i
);
475 retval
= pyra_get_profile_buttons(usb_dev
,
476 &pyra
->profile_buttons
[i
], i
);
481 profile_activated(pyra
, pyra
->settings
.startup_profile
);
486 static int pyra_init_specials(struct hid_device
*hdev
)
488 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
489 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
490 struct pyra_device
*pyra
;
493 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
494 == USB_INTERFACE_PROTOCOL_MOUSE
) {
496 pyra
= kzalloc(sizeof(*pyra
), GFP_KERNEL
);
498 hid_err(hdev
, "can't alloc device descriptor\n");
501 hid_set_drvdata(hdev
, pyra
);
503 retval
= pyra_init_pyra_device_struct(usb_dev
, pyra
);
505 hid_err(hdev
, "couldn't init struct pyra_device\n");
509 retval
= roccat_connect(pyra_class
, hdev
,
510 sizeof(struct pyra_roccat_report
));
512 hid_err(hdev
, "couldn't init char dev\n");
514 pyra
->chrdev_minor
= retval
;
515 pyra
->roccat_claimed
= 1;
518 hid_set_drvdata(hdev
, NULL
);
527 static void pyra_remove_specials(struct hid_device
*hdev
)
529 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
530 struct pyra_device
*pyra
;
532 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
533 == USB_INTERFACE_PROTOCOL_MOUSE
) {
534 pyra
= hid_get_drvdata(hdev
);
535 if (pyra
->roccat_claimed
)
536 roccat_disconnect(pyra
->chrdev_minor
);
537 kfree(hid_get_drvdata(hdev
));
541 static int pyra_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
545 retval
= hid_parse(hdev
);
547 hid_err(hdev
, "parse failed\n");
551 retval
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
553 hid_err(hdev
, "hw start failed\n");
557 retval
= pyra_init_specials(hdev
);
559 hid_err(hdev
, "couldn't install mouse\n");
570 static void pyra_remove(struct hid_device
*hdev
)
572 pyra_remove_specials(hdev
);
576 static void pyra_keep_values_up_to_date(struct pyra_device
*pyra
,
579 struct pyra_mouse_event_button
const *button_event
;
582 case PYRA_MOUSE_REPORT_NUMBER_BUTTON
:
583 button_event
= (struct pyra_mouse_event_button
const *)data
;
584 switch (button_event
->type
) {
585 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2
:
586 profile_activated(pyra
, button_event
->data1
- 1);
588 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI
:
589 pyra
->actual_cpi
= button_event
->data1
;
596 static void pyra_report_to_chrdev(struct pyra_device
const *pyra
,
599 struct pyra_roccat_report roccat_report
;
600 struct pyra_mouse_event_button
const *button_event
;
602 if (data
[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON
)
605 button_event
= (struct pyra_mouse_event_button
const *)data
;
607 switch (button_event
->type
) {
608 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2
:
609 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI
:
610 roccat_report
.type
= button_event
->type
;
611 roccat_report
.value
= button_event
->data1
;
612 roccat_report
.key
= 0;
613 roccat_report_event(pyra
->chrdev_minor
,
614 (uint8_t const *)&roccat_report
);
616 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO
:
617 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT
:
618 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH
:
619 if (button_event
->data2
== PYRA_MOUSE_EVENT_BUTTON_PRESS
) {
620 roccat_report
.type
= button_event
->type
;
621 roccat_report
.key
= button_event
->data1
;
623 * pyra reports profile numbers with range 1-5.
624 * Keeping this behaviour.
626 roccat_report
.value
= pyra
->actual_profile
+ 1;
627 roccat_report_event(pyra
->chrdev_minor
,
628 (uint8_t const *)&roccat_report
);
634 static int pyra_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
637 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
638 struct pyra_device
*pyra
= hid_get_drvdata(hdev
);
640 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
641 != USB_INTERFACE_PROTOCOL_MOUSE
)
644 pyra_keep_values_up_to_date(pyra
, data
);
646 if (pyra
->roccat_claimed
)
647 pyra_report_to_chrdev(pyra
, data
);
652 static const struct hid_device_id pyra_devices
[] = {
653 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
,
654 USB_DEVICE_ID_ROCCAT_PYRA_WIRED
) },
655 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
,
656 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS
) },
660 MODULE_DEVICE_TABLE(hid
, pyra_devices
);
662 static struct hid_driver pyra_driver
= {
664 .id_table
= pyra_devices
,
666 .remove
= pyra_remove
,
667 .raw_event
= pyra_raw_event
670 static int __init
pyra_init(void)
674 /* class name has to be same as driver name */
675 pyra_class
= class_create(THIS_MODULE
, "pyra");
676 if (IS_ERR(pyra_class
))
677 return PTR_ERR(pyra_class
);
678 pyra_class
->dev_attrs
= pyra_attributes
;
679 pyra_class
->dev_bin_attrs
= pyra_bin_attributes
;
681 retval
= hid_register_driver(&pyra_driver
);
683 class_destroy(pyra_class
);
687 static void __exit
pyra_exit(void)
689 hid_unregister_driver(&pyra_driver
);
690 class_destroy(pyra_class
);
693 module_init(pyra_init
);
694 module_exit(pyra_exit
);
696 MODULE_AUTHOR("Stefan Achatz");
697 MODULE_DESCRIPTION("USB Roccat Pyra driver");
698 MODULE_LICENSE("GPL v2");