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 roccat_common2_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 = ROCCAT_COMMON_COMMAND_CONTROL;
53 control.value = value;
54 control.request = request;
56 return roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL,
57 &control, sizeof(struct roccat_common2_control));
60 static int pyra_get_profile_settings(struct usb_device *usb_dev,
61 struct pyra_profile_settings *buf, int number)
64 retval = pyra_send_control(usb_dev, number,
65 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
68 return roccat_common2_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS,
69 buf, sizeof(struct pyra_profile_settings));
72 static int pyra_get_profile_buttons(struct usb_device *usb_dev,
73 struct pyra_profile_buttons *buf, int number)
76 retval = pyra_send_control(usb_dev, number,
77 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
80 return roccat_common2_receive(usb_dev, PYRA_COMMAND_PROFILE_BUTTONS,
81 buf, sizeof(struct pyra_profile_buttons));
84 static int pyra_get_settings(struct usb_device *usb_dev,
85 struct pyra_settings *buf)
87 return roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
88 buf, sizeof(struct pyra_settings));
91 static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf)
93 return roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
94 buf, sizeof(struct pyra_info));
97 static int pyra_set_profile_settings(struct usb_device *usb_dev,
98 struct pyra_profile_settings const *settings)
100 return roccat_common2_send_with_status(usb_dev,
101 PYRA_COMMAND_PROFILE_SETTINGS, settings,
102 sizeof(struct pyra_profile_settings));
105 static int pyra_set_profile_buttons(struct usb_device *usb_dev,
106 struct pyra_profile_buttons const *buttons)
108 return roccat_common2_send_with_status(usb_dev,
109 PYRA_COMMAND_PROFILE_BUTTONS, buttons,
110 sizeof(struct pyra_profile_buttons));
113 static int pyra_set_settings(struct usb_device *usb_dev,
114 struct pyra_settings const *settings)
116 return roccat_common2_send_with_status(usb_dev,
117 PYRA_COMMAND_SETTINGS, settings,
118 sizeof(struct pyra_settings));
121 static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
122 struct kobject *kobj, struct bin_attribute *attr, char *buf,
123 loff_t off, size_t count)
126 container_of(kobj, struct device, kobj)->parent->parent;
127 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
129 if (off >= sizeof(struct pyra_profile_settings))
132 if (off + count > sizeof(struct pyra_profile_settings))
133 count = sizeof(struct pyra_profile_settings) - off;
135 mutex_lock(&pyra->pyra_lock);
136 memcpy(buf, ((char const *)&pyra->profile_settings[*(uint *)(attr->private)]) + off,
138 mutex_unlock(&pyra->pyra_lock);
143 static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
144 struct kobject *kobj, struct bin_attribute *attr, char *buf,
145 loff_t off, size_t count)
148 container_of(kobj, struct device, kobj)->parent->parent;
149 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
151 if (off >= sizeof(struct pyra_profile_buttons))
154 if (off + count > sizeof(struct pyra_profile_buttons))
155 count = sizeof(struct pyra_profile_buttons) - off;
157 mutex_lock(&pyra->pyra_lock);
158 memcpy(buf, ((char const *)&pyra->profile_buttons[*(uint *)(attr->private)]) + off,
160 mutex_unlock(&pyra->pyra_lock);
165 static ssize_t pyra_sysfs_write_profile_settings(struct file *fp,
166 struct kobject *kobj, struct bin_attribute *attr, char *buf,
167 loff_t off, size_t count)
170 container_of(kobj, struct device, kobj)->parent->parent;
171 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
172 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
176 struct pyra_profile_settings *profile_settings;
178 if (off != 0 || count != sizeof(struct pyra_profile_settings))
181 profile_number = ((struct pyra_profile_settings const *)buf)->number;
182 profile_settings = &pyra->profile_settings[profile_number];
184 mutex_lock(&pyra->pyra_lock);
185 difference = memcmp(buf, profile_settings,
186 sizeof(struct pyra_profile_settings));
188 retval = pyra_set_profile_settings(usb_dev,
189 (struct pyra_profile_settings const *)buf);
191 memcpy(profile_settings, buf,
192 sizeof(struct pyra_profile_settings));
194 mutex_unlock(&pyra->pyra_lock);
199 return sizeof(struct pyra_profile_settings);
202 static ssize_t pyra_sysfs_write_profile_buttons(struct file *fp,
203 struct kobject *kobj, struct bin_attribute *attr, char *buf,
204 loff_t off, size_t count)
207 container_of(kobj, struct device, kobj)->parent->parent;
208 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
209 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
213 struct pyra_profile_buttons *profile_buttons;
215 if (off != 0 || count != sizeof(struct pyra_profile_buttons))
218 profile_number = ((struct pyra_profile_buttons const *)buf)->number;
219 profile_buttons = &pyra->profile_buttons[profile_number];
221 mutex_lock(&pyra->pyra_lock);
222 difference = memcmp(buf, profile_buttons,
223 sizeof(struct pyra_profile_buttons));
225 retval = pyra_set_profile_buttons(usb_dev,
226 (struct pyra_profile_buttons const *)buf);
228 memcpy(profile_buttons, buf,
229 sizeof(struct pyra_profile_buttons));
231 mutex_unlock(&pyra->pyra_lock);
236 return sizeof(struct pyra_profile_buttons);
239 static ssize_t pyra_sysfs_read_settings(struct file *fp,
240 struct kobject *kobj, struct bin_attribute *attr, char *buf,
241 loff_t off, size_t count)
244 container_of(kobj, struct device, kobj)->parent->parent;
245 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
247 if (off >= sizeof(struct pyra_settings))
250 if (off + count > sizeof(struct pyra_settings))
251 count = sizeof(struct pyra_settings) - off;
253 mutex_lock(&pyra->pyra_lock);
254 memcpy(buf, ((char const *)&pyra->settings) + off, count);
255 mutex_unlock(&pyra->pyra_lock);
260 static ssize_t pyra_sysfs_write_settings(struct file *fp,
261 struct kobject *kobj, struct bin_attribute *attr, char *buf,
262 loff_t off, size_t count)
265 container_of(kobj, struct device, kobj)->parent->parent;
266 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
267 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
270 struct pyra_roccat_report roccat_report;
272 if (off != 0 || count != sizeof(struct pyra_settings))
275 mutex_lock(&pyra->pyra_lock);
276 difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings));
278 retval = pyra_set_settings(usb_dev,
279 (struct pyra_settings const *)buf);
281 mutex_unlock(&pyra->pyra_lock);
285 memcpy(&pyra->settings, buf,
286 sizeof(struct pyra_settings));
288 profile_activated(pyra, pyra->settings.startup_profile);
290 roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
291 roccat_report.value = pyra->settings.startup_profile + 1;
292 roccat_report.key = 0;
293 roccat_report_event(pyra->chrdev_minor,
294 (uint8_t const *)&roccat_report);
296 mutex_unlock(&pyra->pyra_lock);
297 return sizeof(struct pyra_settings);
301 static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
302 struct device_attribute *attr, char *buf)
304 struct pyra_device *pyra =
305 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
306 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
309 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
310 struct device_attribute *attr, char *buf)
312 struct pyra_device *pyra =
313 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
314 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile);
317 static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
318 struct device_attribute *attr, char *buf)
320 struct pyra_device *pyra =
321 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
322 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version);
325 static ssize_t pyra_sysfs_show_startup_profile(struct device *dev,
326 struct device_attribute *attr, char *buf)
328 struct pyra_device *pyra =
329 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
330 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile);
333 static struct device_attribute pyra_attributes[] = {
334 __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
335 __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
336 __ATTR(firmware_version, 0440,
337 pyra_sysfs_show_firmware_version, NULL),
338 __ATTR(startup_profile, 0440,
339 pyra_sysfs_show_startup_profile, NULL),
343 static struct bin_attribute pyra_bin_attributes[] = {
345 .attr = { .name = "profile_settings", .mode = 0220 },
346 .size = sizeof(struct pyra_profile_settings),
347 .write = pyra_sysfs_write_profile_settings
350 .attr = { .name = "profile1_settings", .mode = 0440 },
351 .size = sizeof(struct pyra_profile_settings),
352 .read = pyra_sysfs_read_profilex_settings,
353 .private = &profile_numbers[0]
356 .attr = { .name = "profile2_settings", .mode = 0440 },
357 .size = sizeof(struct pyra_profile_settings),
358 .read = pyra_sysfs_read_profilex_settings,
359 .private = &profile_numbers[1]
362 .attr = { .name = "profile3_settings", .mode = 0440 },
363 .size = sizeof(struct pyra_profile_settings),
364 .read = pyra_sysfs_read_profilex_settings,
365 .private = &profile_numbers[2]
368 .attr = { .name = "profile4_settings", .mode = 0440 },
369 .size = sizeof(struct pyra_profile_settings),
370 .read = pyra_sysfs_read_profilex_settings,
371 .private = &profile_numbers[3]
374 .attr = { .name = "profile5_settings", .mode = 0440 },
375 .size = sizeof(struct pyra_profile_settings),
376 .read = pyra_sysfs_read_profilex_settings,
377 .private = &profile_numbers[4]
380 .attr = { .name = "profile_buttons", .mode = 0220 },
381 .size = sizeof(struct pyra_profile_buttons),
382 .write = pyra_sysfs_write_profile_buttons
385 .attr = { .name = "profile1_buttons", .mode = 0440 },
386 .size = sizeof(struct pyra_profile_buttons),
387 .read = pyra_sysfs_read_profilex_buttons,
388 .private = &profile_numbers[0]
391 .attr = { .name = "profile2_buttons", .mode = 0440 },
392 .size = sizeof(struct pyra_profile_buttons),
393 .read = pyra_sysfs_read_profilex_buttons,
394 .private = &profile_numbers[1]
397 .attr = { .name = "profile3_buttons", .mode = 0440 },
398 .size = sizeof(struct pyra_profile_buttons),
399 .read = pyra_sysfs_read_profilex_buttons,
400 .private = &profile_numbers[2]
403 .attr = { .name = "profile4_buttons", .mode = 0440 },
404 .size = sizeof(struct pyra_profile_buttons),
405 .read = pyra_sysfs_read_profilex_buttons,
406 .private = &profile_numbers[3]
409 .attr = { .name = "profile5_buttons", .mode = 0440 },
410 .size = sizeof(struct pyra_profile_buttons),
411 .read = pyra_sysfs_read_profilex_buttons,
412 .private = &profile_numbers[4]
415 .attr = { .name = "settings", .mode = 0660 },
416 .size = sizeof(struct pyra_settings),
417 .read = pyra_sysfs_read_settings,
418 .write = pyra_sysfs_write_settings
423 static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
424 struct pyra_device *pyra)
426 struct pyra_info info;
429 mutex_init(&pyra->pyra_lock);
431 retval = pyra_get_info(usb_dev, &info);
435 pyra->firmware_version = info.firmware_version;
437 retval = pyra_get_settings(usb_dev, &pyra->settings);
441 for (i = 0; i < 5; ++i) {
442 retval = pyra_get_profile_settings(usb_dev,
443 &pyra->profile_settings[i], i);
447 retval = pyra_get_profile_buttons(usb_dev,
448 &pyra->profile_buttons[i], i);
453 profile_activated(pyra, pyra->settings.startup_profile);
458 static int pyra_init_specials(struct hid_device *hdev)
460 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
461 struct usb_device *usb_dev = interface_to_usbdev(intf);
462 struct pyra_device *pyra;
465 if (intf->cur_altsetting->desc.bInterfaceProtocol
466 == USB_INTERFACE_PROTOCOL_MOUSE) {
468 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
470 hid_err(hdev, "can't alloc device descriptor\n");
473 hid_set_drvdata(hdev, pyra);
475 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
477 hid_err(hdev, "couldn't init struct pyra_device\n");
481 retval = roccat_connect(pyra_class, hdev,
482 sizeof(struct pyra_roccat_report));
484 hid_err(hdev, "couldn't init char dev\n");
486 pyra->chrdev_minor = retval;
487 pyra->roccat_claimed = 1;
490 hid_set_drvdata(hdev, NULL);
499 static void pyra_remove_specials(struct hid_device *hdev)
501 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
502 struct pyra_device *pyra;
504 if (intf->cur_altsetting->desc.bInterfaceProtocol
505 == USB_INTERFACE_PROTOCOL_MOUSE) {
506 pyra = hid_get_drvdata(hdev);
507 if (pyra->roccat_claimed)
508 roccat_disconnect(pyra->chrdev_minor);
509 kfree(hid_get_drvdata(hdev));
513 static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
517 retval = hid_parse(hdev);
519 hid_err(hdev, "parse failed\n");
523 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
525 hid_err(hdev, "hw start failed\n");
529 retval = pyra_init_specials(hdev);
531 hid_err(hdev, "couldn't install mouse\n");
542 static void pyra_remove(struct hid_device *hdev)
544 pyra_remove_specials(hdev);
548 static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
551 struct pyra_mouse_event_button const *button_event;
554 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
555 button_event = (struct pyra_mouse_event_button const *)data;
556 switch (button_event->type) {
557 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
558 profile_activated(pyra, button_event->data1 - 1);
560 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
561 pyra->actual_cpi = button_event->data1;
568 static void pyra_report_to_chrdev(struct pyra_device const *pyra,
571 struct pyra_roccat_report roccat_report;
572 struct pyra_mouse_event_button const *button_event;
574 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
577 button_event = (struct pyra_mouse_event_button const *)data;
579 switch (button_event->type) {
580 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
581 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
582 roccat_report.type = button_event->type;
583 roccat_report.value = button_event->data1;
584 roccat_report.key = 0;
585 roccat_report_event(pyra->chrdev_minor,
586 (uint8_t const *)&roccat_report);
588 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
589 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
590 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
591 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
592 roccat_report.type = button_event->type;
593 roccat_report.key = button_event->data1;
595 * pyra reports profile numbers with range 1-5.
596 * Keeping this behaviour.
598 roccat_report.value = pyra->actual_profile + 1;
599 roccat_report_event(pyra->chrdev_minor,
600 (uint8_t const *)&roccat_report);
606 static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
609 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
610 struct pyra_device *pyra = hid_get_drvdata(hdev);
612 if (intf->cur_altsetting->desc.bInterfaceProtocol
613 != USB_INTERFACE_PROTOCOL_MOUSE)
619 pyra_keep_values_up_to_date(pyra, data);
621 if (pyra->roccat_claimed)
622 pyra_report_to_chrdev(pyra, data);
627 static const struct hid_device_id pyra_devices[] = {
628 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
629 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
630 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
631 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
635 MODULE_DEVICE_TABLE(hid, pyra_devices);
637 static struct hid_driver pyra_driver = {
639 .id_table = pyra_devices,
641 .remove = pyra_remove,
642 .raw_event = pyra_raw_event
645 static int __init pyra_init(void)
649 /* class name has to be same as driver name */
650 pyra_class = class_create(THIS_MODULE, "pyra");
651 if (IS_ERR(pyra_class))
652 return PTR_ERR(pyra_class);
653 pyra_class->dev_attrs = pyra_attributes;
654 pyra_class->dev_bin_attrs = pyra_bin_attributes;
656 retval = hid_register_driver(&pyra_driver);
658 class_destroy(pyra_class);
662 static void __exit pyra_exit(void)
664 hid_unregister_driver(&pyra_driver);
665 class_destroy(pyra_class);
668 module_init(pyra_init);
669 module_exit(pyra_exit);
671 MODULE_AUTHOR("Stefan Achatz");
672 MODULE_DESCRIPTION("USB Roccat Pyra driver");
673 MODULE_LICENSE("GPL v2");