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 if (new_profile
>= ARRAY_SIZE(pyra
->profile_settings
))
40 pyra
->actual_profile
= new_profile
;
41 pyra
->actual_cpi
= pyra
->profile_settings
[pyra
->actual_profile
].y_cpi
;
44 static int pyra_send_control(struct usb_device
*usb_dev
, int value
,
45 enum pyra_control_requests request
)
47 struct roccat_common2_control control
;
49 if ((request
== PYRA_CONTROL_REQUEST_PROFILE_SETTINGS
||
50 request
== PYRA_CONTROL_REQUEST_PROFILE_BUTTONS
) &&
51 (value
< 0 || value
> 4))
54 control
.command
= ROCCAT_COMMON_COMMAND_CONTROL
;
55 control
.value
= value
;
56 control
.request
= request
;
58 return roccat_common2_send(usb_dev
, ROCCAT_COMMON_COMMAND_CONTROL
,
59 &control
, sizeof(struct roccat_common2_control
));
62 static int pyra_get_profile_settings(struct usb_device
*usb_dev
,
63 struct pyra_profile_settings
*buf
, int number
)
66 retval
= pyra_send_control(usb_dev
, number
,
67 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS
);
70 return roccat_common2_receive(usb_dev
, PYRA_COMMAND_PROFILE_SETTINGS
,
71 buf
, PYRA_SIZE_PROFILE_SETTINGS
);
74 static int pyra_get_settings(struct usb_device
*usb_dev
,
75 struct pyra_settings
*buf
)
77 return roccat_common2_receive(usb_dev
, PYRA_COMMAND_SETTINGS
,
78 buf
, PYRA_SIZE_SETTINGS
);
81 static int pyra_set_settings(struct usb_device
*usb_dev
,
82 struct pyra_settings
const *settings
)
84 return roccat_common2_send_with_status(usb_dev
,
85 PYRA_COMMAND_SETTINGS
, settings
,
89 static ssize_t
pyra_sysfs_read(struct file
*fp
, struct kobject
*kobj
,
90 char *buf
, loff_t off
, size_t count
,
91 size_t real_size
, uint command
)
94 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
95 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
96 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
102 if (off
!= 0 || count
!= real_size
)
105 mutex_lock(&pyra
->pyra_lock
);
106 retval
= roccat_common2_receive(usb_dev
, command
, buf
, real_size
);
107 mutex_unlock(&pyra
->pyra_lock
);
115 static ssize_t
pyra_sysfs_write(struct file
*fp
, struct kobject
*kobj
,
116 void const *buf
, loff_t off
, size_t count
,
117 size_t real_size
, uint command
)
120 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
121 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
122 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
125 if (off
!= 0 || count
!= real_size
)
128 mutex_lock(&pyra
->pyra_lock
);
129 retval
= roccat_common2_send_with_status(usb_dev
, command
, (void *)buf
, real_size
);
130 mutex_unlock(&pyra
->pyra_lock
);
138 #define PYRA_SYSFS_W(thingy, THINGY) \
139 static ssize_t pyra_sysfs_write_ ## thingy(struct file *fp, \
140 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
141 loff_t off, size_t count) \
143 return pyra_sysfs_write(fp, kobj, buf, off, count, \
144 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
147 #define PYRA_SYSFS_R(thingy, THINGY) \
148 static ssize_t pyra_sysfs_read_ ## thingy(struct file *fp, \
149 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
150 loff_t off, size_t count) \
152 return pyra_sysfs_read(fp, kobj, buf, off, count, \
153 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
156 #define PYRA_SYSFS_RW(thingy, THINGY) \
157 PYRA_SYSFS_W(thingy, THINGY) \
158 PYRA_SYSFS_R(thingy, THINGY)
160 #define PYRA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
162 .attr = { .name = #thingy, .mode = 0660 }, \
163 .size = PYRA_SIZE_ ## THINGY, \
164 .read = pyra_sysfs_read_ ## thingy, \
165 .write = pyra_sysfs_write_ ## thingy \
168 #define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \
170 .attr = { .name = #thingy, .mode = 0440 }, \
171 .size = PYRA_SIZE_ ## THINGY, \
172 .read = pyra_sysfs_read_ ## thingy, \
175 #define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \
177 .attr = { .name = #thingy, .mode = 0220 }, \
178 .size = PYRA_SIZE_ ## THINGY, \
179 .write = pyra_sysfs_write_ ## thingy \
182 PYRA_SYSFS_W(control
, CONTROL
)
183 PYRA_SYSFS_RW(info
, INFO
)
184 PYRA_SYSFS_RW(profile_settings
, PROFILE_SETTINGS
)
185 PYRA_SYSFS_RW(profile_buttons
, PROFILE_BUTTONS
)
186 PYRA_SYSFS_R(settings
, SETTINGS
)
188 static ssize_t
pyra_sysfs_read_profilex_settings(struct file
*fp
,
189 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
190 loff_t off
, size_t count
)
193 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
194 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
197 retval
= pyra_send_control(usb_dev
, *(uint
*)(attr
->private),
198 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS
);
202 return pyra_sysfs_read(fp
, kobj
, buf
, off
, count
,
203 PYRA_SIZE_PROFILE_SETTINGS
,
204 PYRA_COMMAND_PROFILE_SETTINGS
);
207 static ssize_t
pyra_sysfs_read_profilex_buttons(struct file
*fp
,
208 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
209 loff_t off
, size_t count
)
212 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
213 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
216 retval
= pyra_send_control(usb_dev
, *(uint
*)(attr
->private),
217 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS
);
221 return pyra_sysfs_read(fp
, kobj
, buf
, off
, count
,
222 PYRA_SIZE_PROFILE_BUTTONS
,
223 PYRA_COMMAND_PROFILE_BUTTONS
);
226 static ssize_t
pyra_sysfs_write_settings(struct file
*fp
,
227 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
228 loff_t off
, size_t count
)
231 container_of(kobj
, struct device
, kobj
)->parent
->parent
;
232 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
233 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
235 struct pyra_roccat_report roccat_report
;
236 struct pyra_settings
const *settings
;
238 if (off
!= 0 || count
!= PYRA_SIZE_SETTINGS
)
241 settings
= (struct pyra_settings
const *)buf
;
242 if (settings
->startup_profile
>= ARRAY_SIZE(pyra
->profile_settings
))
245 mutex_lock(&pyra
->pyra_lock
);
247 retval
= pyra_set_settings(usb_dev
, settings
);
249 mutex_unlock(&pyra
->pyra_lock
);
253 profile_activated(pyra
, settings
->startup_profile
);
255 roccat_report
.type
= PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2
;
256 roccat_report
.value
= settings
->startup_profile
+ 1;
257 roccat_report
.key
= 0;
258 roccat_report_event(pyra
->chrdev_minor
,
259 (uint8_t const *)&roccat_report
);
261 mutex_unlock(&pyra
->pyra_lock
);
262 return PYRA_SIZE_SETTINGS
;
266 static ssize_t
pyra_sysfs_show_actual_cpi(struct device
*dev
,
267 struct device_attribute
*attr
, char *buf
)
269 struct pyra_device
*pyra
=
270 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
271 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->actual_cpi
);
274 static ssize_t
pyra_sysfs_show_actual_profile(struct device
*dev
,
275 struct device_attribute
*attr
, char *buf
)
277 struct pyra_device
*pyra
=
278 hid_get_drvdata(dev_get_drvdata(dev
->parent
->parent
));
279 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
280 struct pyra_settings settings
;
282 mutex_lock(&pyra
->pyra_lock
);
283 roccat_common2_receive(usb_dev
, PYRA_COMMAND_SETTINGS
,
284 &settings
, PYRA_SIZE_SETTINGS
);
285 mutex_unlock(&pyra
->pyra_lock
);
287 return snprintf(buf
, PAGE_SIZE
, "%d\n", settings
.startup_profile
);
290 static ssize_t
pyra_sysfs_show_firmware_version(struct device
*dev
,
291 struct device_attribute
*attr
, char *buf
)
293 struct pyra_device
*pyra
;
294 struct usb_device
*usb_dev
;
295 struct pyra_info info
;
297 dev
= dev
->parent
->parent
;
298 pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
299 usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
301 mutex_lock(&pyra
->pyra_lock
);
302 roccat_common2_receive(usb_dev
, PYRA_COMMAND_INFO
,
303 &info
, PYRA_SIZE_INFO
);
304 mutex_unlock(&pyra
->pyra_lock
);
306 return snprintf(buf
, PAGE_SIZE
, "%d\n", info
.firmware_version
);
309 static struct device_attribute pyra_attributes
[] = {
310 __ATTR(actual_cpi
, 0440, pyra_sysfs_show_actual_cpi
, NULL
),
311 __ATTR(actual_profile
, 0440, pyra_sysfs_show_actual_profile
, NULL
),
312 __ATTR(firmware_version
, 0440,
313 pyra_sysfs_show_firmware_version
, NULL
),
314 __ATTR(startup_profile
, 0440,
315 pyra_sysfs_show_actual_profile
, NULL
),
319 static struct bin_attribute pyra_bin_attributes
[] = {
320 PYRA_BIN_ATTRIBUTE_W(control
, CONTROL
),
321 PYRA_BIN_ATTRIBUTE_RW(info
, INFO
),
322 PYRA_BIN_ATTRIBUTE_RW(profile_settings
, PROFILE_SETTINGS
),
323 PYRA_BIN_ATTRIBUTE_RW(profile_buttons
, PROFILE_BUTTONS
),
324 PYRA_BIN_ATTRIBUTE_RW(settings
, SETTINGS
),
326 .attr
= { .name
= "profile1_settings", .mode
= 0440 },
327 .size
= PYRA_SIZE_PROFILE_SETTINGS
,
328 .read
= pyra_sysfs_read_profilex_settings
,
329 .private = &profile_numbers
[0]
332 .attr
= { .name
= "profile2_settings", .mode
= 0440 },
333 .size
= PYRA_SIZE_PROFILE_SETTINGS
,
334 .read
= pyra_sysfs_read_profilex_settings
,
335 .private = &profile_numbers
[1]
338 .attr
= { .name
= "profile3_settings", .mode
= 0440 },
339 .size
= PYRA_SIZE_PROFILE_SETTINGS
,
340 .read
= pyra_sysfs_read_profilex_settings
,
341 .private = &profile_numbers
[2]
344 .attr
= { .name
= "profile4_settings", .mode
= 0440 },
345 .size
= PYRA_SIZE_PROFILE_SETTINGS
,
346 .read
= pyra_sysfs_read_profilex_settings
,
347 .private = &profile_numbers
[3]
350 .attr
= { .name
= "profile5_settings", .mode
= 0440 },
351 .size
= PYRA_SIZE_PROFILE_SETTINGS
,
352 .read
= pyra_sysfs_read_profilex_settings
,
353 .private = &profile_numbers
[4]
356 .attr
= { .name
= "profile1_buttons", .mode
= 0440 },
357 .size
= PYRA_SIZE_PROFILE_BUTTONS
,
358 .read
= pyra_sysfs_read_profilex_buttons
,
359 .private = &profile_numbers
[0]
362 .attr
= { .name
= "profile2_buttons", .mode
= 0440 },
363 .size
= PYRA_SIZE_PROFILE_BUTTONS
,
364 .read
= pyra_sysfs_read_profilex_buttons
,
365 .private = &profile_numbers
[1]
368 .attr
= { .name
= "profile3_buttons", .mode
= 0440 },
369 .size
= PYRA_SIZE_PROFILE_BUTTONS
,
370 .read
= pyra_sysfs_read_profilex_buttons
,
371 .private = &profile_numbers
[2]
374 .attr
= { .name
= "profile4_buttons", .mode
= 0440 },
375 .size
= PYRA_SIZE_PROFILE_BUTTONS
,
376 .read
= pyra_sysfs_read_profilex_buttons
,
377 .private = &profile_numbers
[3]
380 .attr
= { .name
= "profile5_buttons", .mode
= 0440 },
381 .size
= PYRA_SIZE_PROFILE_BUTTONS
,
382 .read
= pyra_sysfs_read_profilex_buttons
,
383 .private = &profile_numbers
[4]
388 static int pyra_init_pyra_device_struct(struct usb_device
*usb_dev
,
389 struct pyra_device
*pyra
)
391 struct pyra_settings settings
;
394 mutex_init(&pyra
->pyra_lock
);
396 retval
= pyra_get_settings(usb_dev
, &settings
);
400 for (i
= 0; i
< 5; ++i
) {
401 retval
= pyra_get_profile_settings(usb_dev
,
402 &pyra
->profile_settings
[i
], i
);
407 profile_activated(pyra
, settings
.startup_profile
);
412 static int pyra_init_specials(struct hid_device
*hdev
)
414 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
415 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
416 struct pyra_device
*pyra
;
419 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
420 == USB_INTERFACE_PROTOCOL_MOUSE
) {
422 pyra
= kzalloc(sizeof(*pyra
), GFP_KERNEL
);
424 hid_err(hdev
, "can't alloc device descriptor\n");
427 hid_set_drvdata(hdev
, pyra
);
429 retval
= pyra_init_pyra_device_struct(usb_dev
, pyra
);
431 hid_err(hdev
, "couldn't init struct pyra_device\n");
435 retval
= roccat_connect(pyra_class
, hdev
,
436 sizeof(struct pyra_roccat_report
));
438 hid_err(hdev
, "couldn't init char dev\n");
440 pyra
->chrdev_minor
= retval
;
441 pyra
->roccat_claimed
= 1;
444 hid_set_drvdata(hdev
, NULL
);
453 static void pyra_remove_specials(struct hid_device
*hdev
)
455 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
456 struct pyra_device
*pyra
;
458 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
459 == USB_INTERFACE_PROTOCOL_MOUSE
) {
460 pyra
= hid_get_drvdata(hdev
);
461 if (pyra
->roccat_claimed
)
462 roccat_disconnect(pyra
->chrdev_minor
);
463 kfree(hid_get_drvdata(hdev
));
467 static int pyra_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
471 retval
= hid_parse(hdev
);
473 hid_err(hdev
, "parse failed\n");
477 retval
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
479 hid_err(hdev
, "hw start failed\n");
483 retval
= pyra_init_specials(hdev
);
485 hid_err(hdev
, "couldn't install mouse\n");
496 static void pyra_remove(struct hid_device
*hdev
)
498 pyra_remove_specials(hdev
);
502 static void pyra_keep_values_up_to_date(struct pyra_device
*pyra
,
505 struct pyra_mouse_event_button
const *button_event
;
508 case PYRA_MOUSE_REPORT_NUMBER_BUTTON
:
509 button_event
= (struct pyra_mouse_event_button
const *)data
;
510 switch (button_event
->type
) {
511 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2
:
512 profile_activated(pyra
, button_event
->data1
- 1);
514 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI
:
515 pyra
->actual_cpi
= button_event
->data1
;
522 static void pyra_report_to_chrdev(struct pyra_device
const *pyra
,
525 struct pyra_roccat_report roccat_report
;
526 struct pyra_mouse_event_button
const *button_event
;
528 if (data
[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON
)
531 button_event
= (struct pyra_mouse_event_button
const *)data
;
533 switch (button_event
->type
) {
534 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2
:
535 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI
:
536 roccat_report
.type
= button_event
->type
;
537 roccat_report
.value
= button_event
->data1
;
538 roccat_report
.key
= 0;
539 roccat_report_event(pyra
->chrdev_minor
,
540 (uint8_t const *)&roccat_report
);
542 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO
:
543 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT
:
544 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH
:
545 if (button_event
->data2
== PYRA_MOUSE_EVENT_BUTTON_PRESS
) {
546 roccat_report
.type
= button_event
->type
;
547 roccat_report
.key
= button_event
->data1
;
549 * pyra reports profile numbers with range 1-5.
550 * Keeping this behaviour.
552 roccat_report
.value
= pyra
->actual_profile
+ 1;
553 roccat_report_event(pyra
->chrdev_minor
,
554 (uint8_t const *)&roccat_report
);
560 static int pyra_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
563 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
564 struct pyra_device
*pyra
= hid_get_drvdata(hdev
);
566 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
567 != USB_INTERFACE_PROTOCOL_MOUSE
)
573 pyra_keep_values_up_to_date(pyra
, data
);
575 if (pyra
->roccat_claimed
)
576 pyra_report_to_chrdev(pyra
, data
);
581 static const struct hid_device_id pyra_devices
[] = {
582 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
,
583 USB_DEVICE_ID_ROCCAT_PYRA_WIRED
) },
584 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
,
585 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS
) },
589 MODULE_DEVICE_TABLE(hid
, pyra_devices
);
591 static struct hid_driver pyra_driver
= {
593 .id_table
= pyra_devices
,
595 .remove
= pyra_remove
,
596 .raw_event
= pyra_raw_event
599 static int __init
pyra_init(void)
603 /* class name has to be same as driver name */
604 pyra_class
= class_create(THIS_MODULE
, "pyra");
605 if (IS_ERR(pyra_class
))
606 return PTR_ERR(pyra_class
);
607 pyra_class
->dev_attrs
= pyra_attributes
;
608 pyra_class
->dev_bin_attrs
= pyra_bin_attributes
;
610 retval
= hid_register_driver(&pyra_driver
);
612 class_destroy(pyra_class
);
616 static void __exit
pyra_exit(void)
618 hid_unregister_driver(&pyra_driver
);
619 class_destroy(pyra_class
);
622 module_init(pyra_init
);
623 module_exit(pyra_exit
);
625 MODULE_AUTHOR("Stefan Achatz");
626 MODULE_DESCRIPTION("USB Roccat Pyra driver");
627 MODULE_LICENSE("GPL v2");