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 pyra_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 = PYRA_COMMAND_CONTROL;
55 control.value = value;
56 control.request = request;
58 return roccat_common_send(usb_dev, PYRA_COMMAND_CONTROL,
59 &control, sizeof(struct pyra_control));
62 static int pyra_receive_control_status(struct usb_device *usb_dev)
65 struct pyra_control control;
69 retval = roccat_common_receive(usb_dev, PYRA_COMMAND_CONTROL,
70 &control, sizeof(struct pyra_control));
72 /* requested too early, try again */
73 } while (retval == -EPROTO);
75 if (!retval && control.command == PYRA_COMMAND_CONTROL &&
76 control.request == PYRA_CONTROL_REQUEST_STATUS &&
80 hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n",
81 control.request, control.value);
82 return retval ? retval : -EINVAL;
86 static int pyra_get_profile_settings(struct usb_device *usb_dev,
87 struct pyra_profile_settings *buf, int number)
90 retval = pyra_send_control(usb_dev, number,
91 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
94 return roccat_common_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS,
95 buf, sizeof(struct pyra_profile_settings));
98 static int pyra_get_profile_buttons(struct usb_device *usb_dev,
99 struct pyra_profile_buttons *buf, int number)
102 retval = pyra_send_control(usb_dev, number,
103 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
106 return roccat_common_receive(usb_dev, PYRA_COMMAND_PROFILE_BUTTONS,
107 buf, sizeof(struct pyra_profile_buttons));
110 static int pyra_get_settings(struct usb_device *usb_dev,
111 struct pyra_settings *buf)
113 return roccat_common_receive(usb_dev, PYRA_COMMAND_SETTINGS,
114 buf, sizeof(struct pyra_settings));
117 static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf)
119 return roccat_common_receive(usb_dev, PYRA_COMMAND_INFO,
120 buf, sizeof(struct pyra_info));
123 static int pyra_send(struct usb_device *usb_dev, uint command,
124 void const *buf, uint size)
127 retval = roccat_common_send(usb_dev, command, buf, size);
130 return pyra_receive_control_status(usb_dev);
133 static int pyra_set_profile_settings(struct usb_device *usb_dev,
134 struct pyra_profile_settings const *settings)
136 return pyra_send(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS, settings,
137 sizeof(struct pyra_profile_settings));
140 static int pyra_set_profile_buttons(struct usb_device *usb_dev,
141 struct pyra_profile_buttons const *buttons)
143 return pyra_send(usb_dev, PYRA_COMMAND_PROFILE_BUTTONS, buttons,
144 sizeof(struct pyra_profile_buttons));
147 static int pyra_set_settings(struct usb_device *usb_dev,
148 struct pyra_settings const *settings)
150 return pyra_send(usb_dev, PYRA_COMMAND_SETTINGS, settings,
151 sizeof(struct pyra_settings));
154 static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
155 struct kobject *kobj, struct bin_attribute *attr, char *buf,
156 loff_t off, size_t count)
159 container_of(kobj, struct device, kobj)->parent->parent;
160 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
162 if (off >= sizeof(struct pyra_profile_settings))
165 if (off + count > sizeof(struct pyra_profile_settings))
166 count = sizeof(struct pyra_profile_settings) - off;
168 mutex_lock(&pyra->pyra_lock);
169 memcpy(buf, ((char const *)&pyra->profile_settings[*(uint *)(attr->private)]) + off,
171 mutex_unlock(&pyra->pyra_lock);
176 static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
177 struct kobject *kobj, struct bin_attribute *attr, char *buf,
178 loff_t off, size_t count)
181 container_of(kobj, struct device, kobj)->parent->parent;
182 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
184 if (off >= sizeof(struct pyra_profile_buttons))
187 if (off + count > sizeof(struct pyra_profile_buttons))
188 count = sizeof(struct pyra_profile_buttons) - off;
190 mutex_lock(&pyra->pyra_lock);
191 memcpy(buf, ((char const *)&pyra->profile_buttons[*(uint *)(attr->private)]) + off,
193 mutex_unlock(&pyra->pyra_lock);
198 static ssize_t pyra_sysfs_write_profile_settings(struct file *fp,
199 struct kobject *kobj, struct bin_attribute *attr, char *buf,
200 loff_t off, size_t count)
203 container_of(kobj, struct device, kobj)->parent->parent;
204 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
205 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
209 struct pyra_profile_settings *profile_settings;
211 if (off != 0 || count != sizeof(struct pyra_profile_settings))
214 profile_number = ((struct pyra_profile_settings const *)buf)->number;
215 profile_settings = &pyra->profile_settings[profile_number];
217 mutex_lock(&pyra->pyra_lock);
218 difference = memcmp(buf, profile_settings,
219 sizeof(struct pyra_profile_settings));
221 retval = pyra_set_profile_settings(usb_dev,
222 (struct pyra_profile_settings const *)buf);
224 memcpy(profile_settings, buf,
225 sizeof(struct pyra_profile_settings));
227 mutex_unlock(&pyra->pyra_lock);
232 return sizeof(struct pyra_profile_settings);
235 static ssize_t pyra_sysfs_write_profile_buttons(struct file *fp,
236 struct kobject *kobj, struct bin_attribute *attr, char *buf,
237 loff_t off, size_t count)
240 container_of(kobj, struct device, kobj)->parent->parent;
241 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
242 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
246 struct pyra_profile_buttons *profile_buttons;
248 if (off != 0 || count != sizeof(struct pyra_profile_buttons))
251 profile_number = ((struct pyra_profile_buttons const *)buf)->number;
252 profile_buttons = &pyra->profile_buttons[profile_number];
254 mutex_lock(&pyra->pyra_lock);
255 difference = memcmp(buf, profile_buttons,
256 sizeof(struct pyra_profile_buttons));
258 retval = pyra_set_profile_buttons(usb_dev,
259 (struct pyra_profile_buttons const *)buf);
261 memcpy(profile_buttons, buf,
262 sizeof(struct pyra_profile_buttons));
264 mutex_unlock(&pyra->pyra_lock);
269 return sizeof(struct pyra_profile_buttons);
272 static ssize_t pyra_sysfs_read_settings(struct file *fp,
273 struct kobject *kobj, struct bin_attribute *attr, char *buf,
274 loff_t off, size_t count)
277 container_of(kobj, struct device, kobj)->parent->parent;
278 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
280 if (off >= sizeof(struct pyra_settings))
283 if (off + count > sizeof(struct pyra_settings))
284 count = sizeof(struct pyra_settings) - off;
286 mutex_lock(&pyra->pyra_lock);
287 memcpy(buf, ((char const *)&pyra->settings) + off, count);
288 mutex_unlock(&pyra->pyra_lock);
293 static ssize_t pyra_sysfs_write_settings(struct file *fp,
294 struct kobject *kobj, struct bin_attribute *attr, char *buf,
295 loff_t off, size_t count)
298 container_of(kobj, struct device, kobj)->parent->parent;
299 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
300 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
303 struct pyra_roccat_report roccat_report;
305 if (off != 0 || count != sizeof(struct pyra_settings))
308 if (((struct pyra_settings const *)buf)->startup_profile >=
309 ARRAY_SIZE(pyra->profile_settings))
312 mutex_lock(&pyra->pyra_lock);
313 difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings));
315 retval = pyra_set_settings(usb_dev,
316 (struct pyra_settings const *)buf);
318 mutex_unlock(&pyra->pyra_lock);
322 memcpy(&pyra->settings, buf,
323 sizeof(struct pyra_settings));
325 profile_activated(pyra, pyra->settings.startup_profile);
327 roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
328 roccat_report.value = pyra->settings.startup_profile + 1;
329 roccat_report.key = 0;
330 roccat_report_event(pyra->chrdev_minor,
331 (uint8_t const *)&roccat_report);
333 mutex_unlock(&pyra->pyra_lock);
334 return sizeof(struct pyra_settings);
338 static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
339 struct device_attribute *attr, char *buf)
341 struct pyra_device *pyra =
342 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
343 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
346 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
347 struct device_attribute *attr, char *buf)
349 struct pyra_device *pyra =
350 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
351 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile);
354 static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
355 struct device_attribute *attr, char *buf)
357 struct pyra_device *pyra =
358 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
359 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version);
362 static ssize_t pyra_sysfs_show_startup_profile(struct device *dev,
363 struct device_attribute *attr, char *buf)
365 struct pyra_device *pyra =
366 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
367 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile);
370 static struct device_attribute pyra_attributes[] = {
371 __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
372 __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
373 __ATTR(firmware_version, 0440,
374 pyra_sysfs_show_firmware_version, NULL),
375 __ATTR(startup_profile, 0440,
376 pyra_sysfs_show_startup_profile, NULL),
380 static struct bin_attribute pyra_bin_attributes[] = {
382 .attr = { .name = "profile_settings", .mode = 0220 },
383 .size = sizeof(struct pyra_profile_settings),
384 .write = pyra_sysfs_write_profile_settings
387 .attr = { .name = "profile1_settings", .mode = 0440 },
388 .size = sizeof(struct pyra_profile_settings),
389 .read = pyra_sysfs_read_profilex_settings,
390 .private = &profile_numbers[0]
393 .attr = { .name = "profile2_settings", .mode = 0440 },
394 .size = sizeof(struct pyra_profile_settings),
395 .read = pyra_sysfs_read_profilex_settings,
396 .private = &profile_numbers[1]
399 .attr = { .name = "profile3_settings", .mode = 0440 },
400 .size = sizeof(struct pyra_profile_settings),
401 .read = pyra_sysfs_read_profilex_settings,
402 .private = &profile_numbers[2]
405 .attr = { .name = "profile4_settings", .mode = 0440 },
406 .size = sizeof(struct pyra_profile_settings),
407 .read = pyra_sysfs_read_profilex_settings,
408 .private = &profile_numbers[3]
411 .attr = { .name = "profile5_settings", .mode = 0440 },
412 .size = sizeof(struct pyra_profile_settings),
413 .read = pyra_sysfs_read_profilex_settings,
414 .private = &profile_numbers[4]
417 .attr = { .name = "profile_buttons", .mode = 0220 },
418 .size = sizeof(struct pyra_profile_buttons),
419 .write = pyra_sysfs_write_profile_buttons
422 .attr = { .name = "profile1_buttons", .mode = 0440 },
423 .size = sizeof(struct pyra_profile_buttons),
424 .read = pyra_sysfs_read_profilex_buttons,
425 .private = &profile_numbers[0]
428 .attr = { .name = "profile2_buttons", .mode = 0440 },
429 .size = sizeof(struct pyra_profile_buttons),
430 .read = pyra_sysfs_read_profilex_buttons,
431 .private = &profile_numbers[1]
434 .attr = { .name = "profile3_buttons", .mode = 0440 },
435 .size = sizeof(struct pyra_profile_buttons),
436 .read = pyra_sysfs_read_profilex_buttons,
437 .private = &profile_numbers[2]
440 .attr = { .name = "profile4_buttons", .mode = 0440 },
441 .size = sizeof(struct pyra_profile_buttons),
442 .read = pyra_sysfs_read_profilex_buttons,
443 .private = &profile_numbers[3]
446 .attr = { .name = "profile5_buttons", .mode = 0440 },
447 .size = sizeof(struct pyra_profile_buttons),
448 .read = pyra_sysfs_read_profilex_buttons,
449 .private = &profile_numbers[4]
452 .attr = { .name = "settings", .mode = 0660 },
453 .size = sizeof(struct pyra_settings),
454 .read = pyra_sysfs_read_settings,
455 .write = pyra_sysfs_write_settings
460 static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
461 struct pyra_device *pyra)
463 struct pyra_info info;
466 mutex_init(&pyra->pyra_lock);
468 retval = pyra_get_info(usb_dev, &info);
472 pyra->firmware_version = info.firmware_version;
474 retval = pyra_get_settings(usb_dev, &pyra->settings);
478 for (i = 0; i < 5; ++i) {
479 retval = pyra_get_profile_settings(usb_dev,
480 &pyra->profile_settings[i], i);
484 retval = pyra_get_profile_buttons(usb_dev,
485 &pyra->profile_buttons[i], i);
490 profile_activated(pyra, pyra->settings.startup_profile);
495 static int pyra_init_specials(struct hid_device *hdev)
497 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
498 struct usb_device *usb_dev = interface_to_usbdev(intf);
499 struct pyra_device *pyra;
502 if (intf->cur_altsetting->desc.bInterfaceProtocol
503 == USB_INTERFACE_PROTOCOL_MOUSE) {
505 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
507 hid_err(hdev, "can't alloc device descriptor\n");
510 hid_set_drvdata(hdev, pyra);
512 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
514 hid_err(hdev, "couldn't init struct pyra_device\n");
518 retval = roccat_connect(pyra_class, hdev,
519 sizeof(struct pyra_roccat_report));
521 hid_err(hdev, "couldn't init char dev\n");
523 pyra->chrdev_minor = retval;
524 pyra->roccat_claimed = 1;
527 hid_set_drvdata(hdev, NULL);
536 static void pyra_remove_specials(struct hid_device *hdev)
538 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
539 struct pyra_device *pyra;
541 if (intf->cur_altsetting->desc.bInterfaceProtocol
542 == USB_INTERFACE_PROTOCOL_MOUSE) {
543 pyra = hid_get_drvdata(hdev);
544 if (pyra->roccat_claimed)
545 roccat_disconnect(pyra->chrdev_minor);
546 kfree(hid_get_drvdata(hdev));
550 static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
554 retval = hid_parse(hdev);
556 hid_err(hdev, "parse failed\n");
560 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
562 hid_err(hdev, "hw start failed\n");
566 retval = pyra_init_specials(hdev);
568 hid_err(hdev, "couldn't install mouse\n");
579 static void pyra_remove(struct hid_device *hdev)
581 pyra_remove_specials(hdev);
585 static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
588 struct pyra_mouse_event_button const *button_event;
591 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
592 button_event = (struct pyra_mouse_event_button const *)data;
593 switch (button_event->type) {
594 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
595 profile_activated(pyra, button_event->data1 - 1);
597 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
598 pyra->actual_cpi = button_event->data1;
605 static void pyra_report_to_chrdev(struct pyra_device const *pyra,
608 struct pyra_roccat_report roccat_report;
609 struct pyra_mouse_event_button const *button_event;
611 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
614 button_event = (struct pyra_mouse_event_button const *)data;
616 switch (button_event->type) {
617 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
618 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
619 roccat_report.type = button_event->type;
620 roccat_report.value = button_event->data1;
621 roccat_report.key = 0;
622 roccat_report_event(pyra->chrdev_minor,
623 (uint8_t const *)&roccat_report);
625 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
626 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
627 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
628 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
629 roccat_report.type = button_event->type;
630 roccat_report.key = button_event->data1;
632 * pyra reports profile numbers with range 1-5.
633 * Keeping this behaviour.
635 roccat_report.value = pyra->actual_profile + 1;
636 roccat_report_event(pyra->chrdev_minor,
637 (uint8_t const *)&roccat_report);
643 static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
646 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
647 struct pyra_device *pyra = hid_get_drvdata(hdev);
649 if (intf->cur_altsetting->desc.bInterfaceProtocol
650 != USB_INTERFACE_PROTOCOL_MOUSE)
656 pyra_keep_values_up_to_date(pyra, data);
658 if (pyra->roccat_claimed)
659 pyra_report_to_chrdev(pyra, data);
664 static const struct hid_device_id pyra_devices[] = {
665 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
666 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
667 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
668 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
672 MODULE_DEVICE_TABLE(hid, pyra_devices);
674 static struct hid_driver pyra_driver = {
676 .id_table = pyra_devices,
678 .remove = pyra_remove,
679 .raw_event = pyra_raw_event
682 static int __init pyra_init(void)
686 /* class name has to be same as driver name */
687 pyra_class = class_create(THIS_MODULE, "pyra");
688 if (IS_ERR(pyra_class))
689 return PTR_ERR(pyra_class);
690 pyra_class->dev_attrs = pyra_attributes;
691 pyra_class->dev_bin_attrs = pyra_bin_attributes;
693 retval = hid_register_driver(&pyra_driver);
695 class_destroy(pyra_class);
699 static void __exit pyra_exit(void)
701 hid_unregister_driver(&pyra_driver);
702 class_destroy(pyra_class);
705 module_init(pyra_init);
706 module_exit(pyra_exit);
708 MODULE_AUTHOR("Stefan Achatz");
709 MODULE_DESCRIPTION("USB Roccat Pyra driver");
710 MODULE_LICENSE("GPL v2");