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, PYRA_SIZE_PROFILE_SETTINGS);
72 static int pyra_get_settings(struct usb_device *usb_dev,
73 struct pyra_settings *buf)
75 return roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
76 buf, PYRA_SIZE_SETTINGS);
79 static int pyra_set_settings(struct usb_device *usb_dev,
80 struct pyra_settings const *settings)
82 return roccat_common2_send_with_status(usb_dev,
83 PYRA_COMMAND_SETTINGS, settings,
87 static ssize_t pyra_sysfs_read(struct file *fp, struct kobject *kobj,
88 char *buf, loff_t off, size_t count,
89 size_t real_size, uint command)
92 container_of(kobj, struct device, kobj)->parent->parent;
93 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
94 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
100 if (off != 0 || count != real_size)
103 mutex_lock(&pyra->pyra_lock);
104 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
105 mutex_unlock(&pyra->pyra_lock);
113 static ssize_t pyra_sysfs_write(struct file *fp, struct kobject *kobj,
114 void const *buf, loff_t off, size_t count,
115 size_t real_size, uint command)
118 container_of(kobj, struct device, kobj)->parent->parent;
119 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
120 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
123 if (off != 0 || count != real_size)
126 mutex_lock(&pyra->pyra_lock);
127 retval = roccat_common2_send_with_status(usb_dev, command, (void *)buf, real_size);
128 mutex_unlock(&pyra->pyra_lock);
136 #define PYRA_SYSFS_W(thingy, THINGY) \
137 static ssize_t pyra_sysfs_write_ ## thingy(struct file *fp, \
138 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
139 loff_t off, size_t count) \
141 return pyra_sysfs_write(fp, kobj, buf, off, count, \
142 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
145 #define PYRA_SYSFS_R(thingy, THINGY) \
146 static ssize_t pyra_sysfs_read_ ## thingy(struct file *fp, \
147 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
148 loff_t off, size_t count) \
150 return pyra_sysfs_read(fp, kobj, buf, off, count, \
151 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
154 #define PYRA_SYSFS_RW(thingy, THINGY) \
155 PYRA_SYSFS_W(thingy, THINGY) \
156 PYRA_SYSFS_R(thingy, THINGY)
158 #define PYRA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
160 .attr = { .name = #thingy, .mode = 0660 }, \
161 .size = PYRA_SIZE_ ## THINGY, \
162 .read = pyra_sysfs_read_ ## thingy, \
163 .write = pyra_sysfs_write_ ## thingy \
166 #define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \
168 .attr = { .name = #thingy, .mode = 0440 }, \
169 .size = PYRA_SIZE_ ## THINGY, \
170 .read = pyra_sysfs_read_ ## thingy, \
173 #define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \
175 .attr = { .name = #thingy, .mode = 0220 }, \
176 .size = PYRA_SIZE_ ## THINGY, \
177 .write = pyra_sysfs_write_ ## thingy \
180 PYRA_SYSFS_RW(info, INFO)
181 PYRA_SYSFS_W(profile_settings, PROFILE_SETTINGS)
182 PYRA_SYSFS_W(profile_buttons, PROFILE_BUTTONS)
183 PYRA_SYSFS_R(settings, SETTINGS)
185 static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
186 struct kobject *kobj, struct bin_attribute *attr, char *buf,
187 loff_t off, size_t count)
190 container_of(kobj, struct device, kobj)->parent->parent;
191 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
194 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
195 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
199 return pyra_sysfs_read(fp, kobj, buf, off, count,
200 PYRA_SIZE_PROFILE_SETTINGS,
201 PYRA_COMMAND_PROFILE_SETTINGS);
204 static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
205 struct kobject *kobj, struct bin_attribute *attr, char *buf,
206 loff_t off, size_t count)
209 container_of(kobj, struct device, kobj)->parent->parent;
210 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
213 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
214 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
218 return pyra_sysfs_read(fp, kobj, buf, off, count,
219 PYRA_SIZE_PROFILE_BUTTONS,
220 PYRA_COMMAND_PROFILE_BUTTONS);
223 static ssize_t pyra_sysfs_write_settings(struct file *fp,
224 struct kobject *kobj, struct bin_attribute *attr, char *buf,
225 loff_t off, size_t count)
228 container_of(kobj, struct device, kobj)->parent->parent;
229 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
230 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
232 struct pyra_roccat_report roccat_report;
233 struct pyra_settings const *settings;
235 if (off != 0 || count != PYRA_SIZE_SETTINGS)
238 mutex_lock(&pyra->pyra_lock);
240 settings = (struct pyra_settings const *)buf;
242 retval = pyra_set_settings(usb_dev, settings);
244 mutex_unlock(&pyra->pyra_lock);
248 profile_activated(pyra, settings->startup_profile);
250 roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
251 roccat_report.value = settings->startup_profile + 1;
252 roccat_report.key = 0;
253 roccat_report_event(pyra->chrdev_minor,
254 (uint8_t const *)&roccat_report);
256 mutex_unlock(&pyra->pyra_lock);
257 return PYRA_SIZE_SETTINGS;
261 static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
262 struct device_attribute *attr, char *buf)
264 struct pyra_device *pyra =
265 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
266 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
269 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
270 struct device_attribute *attr, char *buf)
272 struct pyra_device *pyra =
273 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
274 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
275 struct pyra_settings settings;
277 mutex_lock(&pyra->pyra_lock);
278 roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
279 &settings, PYRA_SIZE_SETTINGS);
280 mutex_unlock(&pyra->pyra_lock);
282 return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile);
285 static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
286 struct device_attribute *attr, char *buf)
288 struct pyra_device *pyra;
289 struct usb_device *usb_dev;
290 struct pyra_info info;
292 dev = dev->parent->parent;
293 pyra = hid_get_drvdata(dev_get_drvdata(dev));
294 usb_dev = interface_to_usbdev(to_usb_interface(dev));
296 mutex_lock(&pyra->pyra_lock);
297 roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
298 &info, PYRA_SIZE_INFO);
299 mutex_unlock(&pyra->pyra_lock);
301 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
304 static struct device_attribute pyra_attributes[] = {
305 __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
306 __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
307 __ATTR(firmware_version, 0440,
308 pyra_sysfs_show_firmware_version, NULL),
309 __ATTR(startup_profile, 0440,
310 pyra_sysfs_show_actual_profile, NULL),
314 static struct bin_attribute pyra_bin_attributes[] = {
315 PYRA_BIN_ATTRIBUTE_RW(info, INFO),
316 PYRA_BIN_ATTRIBUTE_W(profile_settings, PROFILE_SETTINGS),
317 PYRA_BIN_ATTRIBUTE_W(profile_buttons, PROFILE_BUTTONS),
318 PYRA_BIN_ATTRIBUTE_RW(settings, SETTINGS),
320 .attr = { .name = "profile1_settings", .mode = 0440 },
321 .size = PYRA_SIZE_PROFILE_SETTINGS,
322 .read = pyra_sysfs_read_profilex_settings,
323 .private = &profile_numbers[0]
326 .attr = { .name = "profile2_settings", .mode = 0440 },
327 .size = PYRA_SIZE_PROFILE_SETTINGS,
328 .read = pyra_sysfs_read_profilex_settings,
329 .private = &profile_numbers[1]
332 .attr = { .name = "profile3_settings", .mode = 0440 },
333 .size = PYRA_SIZE_PROFILE_SETTINGS,
334 .read = pyra_sysfs_read_profilex_settings,
335 .private = &profile_numbers[2]
338 .attr = { .name = "profile4_settings", .mode = 0440 },
339 .size = PYRA_SIZE_PROFILE_SETTINGS,
340 .read = pyra_sysfs_read_profilex_settings,
341 .private = &profile_numbers[3]
344 .attr = { .name = "profile5_settings", .mode = 0440 },
345 .size = PYRA_SIZE_PROFILE_SETTINGS,
346 .read = pyra_sysfs_read_profilex_settings,
347 .private = &profile_numbers[4]
350 .attr = { .name = "profile1_buttons", .mode = 0440 },
351 .size = PYRA_SIZE_PROFILE_BUTTONS,
352 .read = pyra_sysfs_read_profilex_buttons,
353 .private = &profile_numbers[0]
356 .attr = { .name = "profile2_buttons", .mode = 0440 },
357 .size = PYRA_SIZE_PROFILE_BUTTONS,
358 .read = pyra_sysfs_read_profilex_buttons,
359 .private = &profile_numbers[1]
362 .attr = { .name = "profile3_buttons", .mode = 0440 },
363 .size = PYRA_SIZE_PROFILE_BUTTONS,
364 .read = pyra_sysfs_read_profilex_buttons,
365 .private = &profile_numbers[2]
368 .attr = { .name = "profile4_buttons", .mode = 0440 },
369 .size = PYRA_SIZE_PROFILE_BUTTONS,
370 .read = pyra_sysfs_read_profilex_buttons,
371 .private = &profile_numbers[3]
374 .attr = { .name = "profile5_buttons", .mode = 0440 },
375 .size = PYRA_SIZE_PROFILE_BUTTONS,
376 .read = pyra_sysfs_read_profilex_buttons,
377 .private = &profile_numbers[4]
382 static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
383 struct pyra_device *pyra)
385 struct pyra_settings settings;
388 mutex_init(&pyra->pyra_lock);
390 retval = pyra_get_settings(usb_dev, &settings);
394 for (i = 0; i < 5; ++i) {
395 retval = pyra_get_profile_settings(usb_dev,
396 &pyra->profile_settings[i], i);
401 profile_activated(pyra, settings.startup_profile);
406 static int pyra_init_specials(struct hid_device *hdev)
408 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
409 struct usb_device *usb_dev = interface_to_usbdev(intf);
410 struct pyra_device *pyra;
413 if (intf->cur_altsetting->desc.bInterfaceProtocol
414 == USB_INTERFACE_PROTOCOL_MOUSE) {
416 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
418 hid_err(hdev, "can't alloc device descriptor\n");
421 hid_set_drvdata(hdev, pyra);
423 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
425 hid_err(hdev, "couldn't init struct pyra_device\n");
429 retval = roccat_connect(pyra_class, hdev,
430 sizeof(struct pyra_roccat_report));
432 hid_err(hdev, "couldn't init char dev\n");
434 pyra->chrdev_minor = retval;
435 pyra->roccat_claimed = 1;
438 hid_set_drvdata(hdev, NULL);
447 static void pyra_remove_specials(struct hid_device *hdev)
449 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
450 struct pyra_device *pyra;
452 if (intf->cur_altsetting->desc.bInterfaceProtocol
453 == USB_INTERFACE_PROTOCOL_MOUSE) {
454 pyra = hid_get_drvdata(hdev);
455 if (pyra->roccat_claimed)
456 roccat_disconnect(pyra->chrdev_minor);
457 kfree(hid_get_drvdata(hdev));
461 static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
465 retval = hid_parse(hdev);
467 hid_err(hdev, "parse failed\n");
471 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
473 hid_err(hdev, "hw start failed\n");
477 retval = pyra_init_specials(hdev);
479 hid_err(hdev, "couldn't install mouse\n");
490 static void pyra_remove(struct hid_device *hdev)
492 pyra_remove_specials(hdev);
496 static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
499 struct pyra_mouse_event_button const *button_event;
502 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
503 button_event = (struct pyra_mouse_event_button const *)data;
504 switch (button_event->type) {
505 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
506 profile_activated(pyra, button_event->data1 - 1);
508 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
509 pyra->actual_cpi = button_event->data1;
516 static void pyra_report_to_chrdev(struct pyra_device const *pyra,
519 struct pyra_roccat_report roccat_report;
520 struct pyra_mouse_event_button const *button_event;
522 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
525 button_event = (struct pyra_mouse_event_button const *)data;
527 switch (button_event->type) {
528 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
529 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
530 roccat_report.type = button_event->type;
531 roccat_report.value = button_event->data1;
532 roccat_report.key = 0;
533 roccat_report_event(pyra->chrdev_minor,
534 (uint8_t const *)&roccat_report);
536 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
537 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
538 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
539 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
540 roccat_report.type = button_event->type;
541 roccat_report.key = button_event->data1;
543 * pyra reports profile numbers with range 1-5.
544 * Keeping this behaviour.
546 roccat_report.value = pyra->actual_profile + 1;
547 roccat_report_event(pyra->chrdev_minor,
548 (uint8_t const *)&roccat_report);
554 static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
557 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
558 struct pyra_device *pyra = hid_get_drvdata(hdev);
560 if (intf->cur_altsetting->desc.bInterfaceProtocol
561 != USB_INTERFACE_PROTOCOL_MOUSE)
567 pyra_keep_values_up_to_date(pyra, data);
569 if (pyra->roccat_claimed)
570 pyra_report_to_chrdev(pyra, data);
575 static const struct hid_device_id pyra_devices[] = {
576 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
577 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
578 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
579 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
583 MODULE_DEVICE_TABLE(hid, pyra_devices);
585 static struct hid_driver pyra_driver = {
587 .id_table = pyra_devices,
589 .remove = pyra_remove,
590 .raw_event = pyra_raw_event
593 static int __init pyra_init(void)
597 /* class name has to be same as driver name */
598 pyra_class = class_create(THIS_MODULE, "pyra");
599 if (IS_ERR(pyra_class))
600 return PTR_ERR(pyra_class);
601 pyra_class->dev_attrs = pyra_attributes;
602 pyra_class->dev_bin_attrs = pyra_bin_attributes;
604 retval = hid_register_driver(&pyra_driver);
606 class_destroy(pyra_class);
610 static void __exit pyra_exit(void)
612 hid_unregister_driver(&pyra_driver);
613 class_destroy(pyra_class);
616 module_init(pyra_init);
617 module_exit(pyra_exit);
619 MODULE_AUTHOR("Stefan Achatz");
620 MODULE_DESCRIPTION("USB Roccat Pyra driver");
621 MODULE_LICENSE("GPL v2");