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/usb.h>
24 #include <linux/module.h>
25 #include <linux/slab.h>
27 #include "hid-roccat.h"
28 #include "hid-roccat-pyra.h"
30 static void profile_activated(struct pyra_device
*pyra
,
31 unsigned int new_profile
)
33 pyra
->actual_profile
= new_profile
;
34 pyra
->actual_cpi
= pyra
->profile_settings
[pyra
->actual_profile
].y_cpi
;
37 static int pyra_send_control(struct usb_device
*usb_dev
, int value
,
38 enum pyra_control_requests request
)
41 struct pyra_control control
;
43 if ((request
== PYRA_CONTROL_REQUEST_PROFILE_SETTINGS
||
44 request
== PYRA_CONTROL_REQUEST_PROFILE_BUTTONS
) &&
45 (value
< 0 || value
> 4))
48 control
.command
= PYRA_COMMAND_CONTROL
;
49 control
.value
= value
;
50 control
.request
= request
;
52 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
53 USB_REQ_SET_CONFIGURATION
,
54 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
55 PYRA_USB_COMMAND_CONTROL
, 0, (char *)&control
,
56 sizeof(struct pyra_control
),
57 USB_CTRL_SET_TIMEOUT
);
59 if (len
!= sizeof(struct pyra_control
))
65 static int pyra_receive_control_status(struct usb_device
*usb_dev
)
68 struct pyra_control control
;
73 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
74 USB_REQ_CLEAR_FEATURE
,
75 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
|
77 PYRA_USB_COMMAND_CONTROL
, 0, (char *)&control
,
78 sizeof(struct pyra_control
),
79 USB_CTRL_SET_TIMEOUT
);
81 /* requested too early, try again */
82 } while (len
== -EPROTO
);
84 if (len
== sizeof(struct pyra_control
) &&
85 control
.command
== PYRA_COMMAND_CONTROL
&&
86 control
.request
== PYRA_CONTROL_REQUEST_STATUS
&&
90 hid_err(usb_dev
, "receive control status: unknown response 0x%x 0x%x\n",
91 control
.request
, control
.value
);
96 static int pyra_get_profile_settings(struct usb_device
*usb_dev
,
97 struct pyra_profile_settings
*buf
, int number
)
101 retval
= pyra_send_control(usb_dev
, number
,
102 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS
);
107 retval
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
108 USB_REQ_CLEAR_FEATURE
,
109 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
110 PYRA_USB_COMMAND_PROFILE_SETTINGS
, 0, (char *)buf
,
111 sizeof(struct pyra_profile_settings
),
112 USB_CTRL_SET_TIMEOUT
);
114 if (retval
!= sizeof(struct pyra_profile_settings
))
120 static int pyra_get_profile_buttons(struct usb_device
*usb_dev
,
121 struct pyra_profile_buttons
*buf
, int number
)
125 retval
= pyra_send_control(usb_dev
, number
,
126 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS
);
131 retval
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
132 USB_REQ_CLEAR_FEATURE
,
133 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
134 PYRA_USB_COMMAND_PROFILE_BUTTONS
, 0, (char *)buf
,
135 sizeof(struct pyra_profile_buttons
),
136 USB_CTRL_SET_TIMEOUT
);
138 if (retval
!= sizeof(struct pyra_profile_buttons
))
144 static int pyra_get_settings(struct usb_device
*usb_dev
,
145 struct pyra_settings
*buf
)
148 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
149 USB_REQ_CLEAR_FEATURE
,
150 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
151 PYRA_USB_COMMAND_SETTINGS
, 0, buf
,
152 sizeof(struct pyra_settings
), USB_CTRL_SET_TIMEOUT
);
153 if (len
!= sizeof(struct pyra_settings
))
158 static int pyra_get_info(struct usb_device
*usb_dev
, struct pyra_info
*buf
)
161 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
162 USB_REQ_CLEAR_FEATURE
,
163 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
164 PYRA_USB_COMMAND_INFO
, 0, buf
,
165 sizeof(struct pyra_info
), USB_CTRL_SET_TIMEOUT
);
166 if (len
!= sizeof(struct pyra_info
))
171 static int pyra_set_profile_settings(struct usb_device
*usb_dev
,
172 struct pyra_profile_settings
const *settings
)
175 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
176 USB_REQ_SET_CONFIGURATION
,
177 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
178 PYRA_USB_COMMAND_PROFILE_SETTINGS
, 0, (char *)settings
,
179 sizeof(struct pyra_profile_settings
),
180 USB_CTRL_SET_TIMEOUT
);
181 if (len
!= sizeof(struct pyra_profile_settings
))
183 if (pyra_receive_control_status(usb_dev
))
188 static int pyra_set_profile_buttons(struct usb_device
*usb_dev
,
189 struct pyra_profile_buttons
const *buttons
)
192 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
193 USB_REQ_SET_CONFIGURATION
,
194 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
195 PYRA_USB_COMMAND_PROFILE_BUTTONS
, 0, (char *)buttons
,
196 sizeof(struct pyra_profile_buttons
),
197 USB_CTRL_SET_TIMEOUT
);
198 if (len
!= sizeof(struct pyra_profile_buttons
))
200 if (pyra_receive_control_status(usb_dev
))
205 static int pyra_set_settings(struct usb_device
*usb_dev
,
206 struct pyra_settings
const *settings
)
209 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
210 USB_REQ_SET_CONFIGURATION
,
211 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
212 PYRA_USB_COMMAND_SETTINGS
, 0, (char *)settings
,
213 sizeof(struct pyra_settings
), USB_CTRL_SET_TIMEOUT
);
214 if (len
!= sizeof(struct pyra_settings
))
216 if (pyra_receive_control_status(usb_dev
))
221 static ssize_t
pyra_sysfs_read_profilex_settings(struct file
*fp
,
222 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
223 loff_t off
, size_t count
, int number
)
225 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
226 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
228 if (off
>= sizeof(struct pyra_profile_settings
))
231 if (off
+ count
> sizeof(struct pyra_profile_settings
))
232 count
= sizeof(struct pyra_profile_settings
) - off
;
234 mutex_lock(&pyra
->pyra_lock
);
235 memcpy(buf
, ((char const *)&pyra
->profile_settings
[number
]) + off
,
237 mutex_unlock(&pyra
->pyra_lock
);
242 static ssize_t
pyra_sysfs_read_profile1_settings(struct file
*fp
,
243 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
244 loff_t off
, size_t count
)
246 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
247 attr
, buf
, off
, count
, 0);
250 static ssize_t
pyra_sysfs_read_profile2_settings(struct file
*fp
,
251 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
252 loff_t off
, size_t count
)
254 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
255 attr
, buf
, off
, count
, 1);
258 static ssize_t
pyra_sysfs_read_profile3_settings(struct file
*fp
,
259 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
260 loff_t off
, size_t count
)
262 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
263 attr
, buf
, off
, count
, 2);
266 static ssize_t
pyra_sysfs_read_profile4_settings(struct file
*fp
,
267 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
268 loff_t off
, size_t count
)
270 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
271 attr
, buf
, off
, count
, 3);
274 static ssize_t
pyra_sysfs_read_profile5_settings(struct file
*fp
,
275 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
276 loff_t off
, size_t count
)
278 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
279 attr
, buf
, off
, count
, 4);
282 static ssize_t
pyra_sysfs_read_profilex_buttons(struct file
*fp
,
283 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
284 loff_t off
, size_t count
, int number
)
286 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
287 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
289 if (off
>= sizeof(struct pyra_profile_buttons
))
292 if (off
+ count
> sizeof(struct pyra_profile_buttons
))
293 count
= sizeof(struct pyra_profile_buttons
) - off
;
295 mutex_lock(&pyra
->pyra_lock
);
296 memcpy(buf
, ((char const *)&pyra
->profile_buttons
[number
]) + off
,
298 mutex_unlock(&pyra
->pyra_lock
);
303 static ssize_t
pyra_sysfs_read_profile1_buttons(struct file
*fp
,
304 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
305 loff_t off
, size_t count
)
307 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
308 attr
, buf
, off
, count
, 0);
311 static ssize_t
pyra_sysfs_read_profile2_buttons(struct file
*fp
,
312 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
313 loff_t off
, size_t count
)
315 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
316 attr
, buf
, off
, count
, 1);
319 static ssize_t
pyra_sysfs_read_profile3_buttons(struct file
*fp
,
320 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
321 loff_t off
, size_t count
)
323 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
324 attr
, buf
, off
, count
, 2);
327 static ssize_t
pyra_sysfs_read_profile4_buttons(struct file
*fp
,
328 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
329 loff_t off
, size_t count
)
331 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
332 attr
, buf
, off
, count
, 3);
335 static ssize_t
pyra_sysfs_read_profile5_buttons(struct file
*fp
,
336 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
337 loff_t off
, size_t count
)
339 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
340 attr
, buf
, off
, count
, 4);
343 static ssize_t
pyra_sysfs_write_profile_settings(struct file
*fp
,
344 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
345 loff_t off
, size_t count
)
347 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
348 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
349 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
353 struct pyra_profile_settings
*profile_settings
;
355 if (off
!= 0 || count
!= sizeof(struct pyra_profile_settings
))
358 profile_number
= ((struct pyra_profile_settings
const *)buf
)->number
;
359 profile_settings
= &pyra
->profile_settings
[profile_number
];
361 mutex_lock(&pyra
->pyra_lock
);
362 difference
= memcmp(buf
, profile_settings
,
363 sizeof(struct pyra_profile_settings
));
365 retval
= pyra_set_profile_settings(usb_dev
,
366 (struct pyra_profile_settings
const *)buf
);
368 memcpy(profile_settings
, buf
,
369 sizeof(struct pyra_profile_settings
));
371 mutex_unlock(&pyra
->pyra_lock
);
376 return sizeof(struct pyra_profile_settings
);
379 static ssize_t
pyra_sysfs_write_profile_buttons(struct file
*fp
,
380 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
381 loff_t off
, size_t count
)
383 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
384 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
385 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
389 struct pyra_profile_buttons
*profile_buttons
;
391 if (off
!= 0 || count
!= sizeof(struct pyra_profile_buttons
))
394 profile_number
= ((struct pyra_profile_buttons
const *)buf
)->number
;
395 profile_buttons
= &pyra
->profile_buttons
[profile_number
];
397 mutex_lock(&pyra
->pyra_lock
);
398 difference
= memcmp(buf
, profile_buttons
,
399 sizeof(struct pyra_profile_buttons
));
401 retval
= pyra_set_profile_buttons(usb_dev
,
402 (struct pyra_profile_buttons
const *)buf
);
404 memcpy(profile_buttons
, buf
,
405 sizeof(struct pyra_profile_buttons
));
407 mutex_unlock(&pyra
->pyra_lock
);
412 return sizeof(struct pyra_profile_buttons
);
415 static ssize_t
pyra_sysfs_read_settings(struct file
*fp
,
416 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
417 loff_t off
, size_t count
)
419 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
420 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
422 if (off
>= sizeof(struct pyra_settings
))
425 if (off
+ count
> sizeof(struct pyra_settings
))
426 count
= sizeof(struct pyra_settings
) - off
;
428 mutex_lock(&pyra
->pyra_lock
);
429 memcpy(buf
, ((char const *)&pyra
->settings
) + off
, count
);
430 mutex_unlock(&pyra
->pyra_lock
);
435 static ssize_t
pyra_sysfs_write_settings(struct file
*fp
,
436 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
437 loff_t off
, size_t count
)
439 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
440 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
441 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
445 if (off
!= 0 || count
!= sizeof(struct pyra_settings
))
448 mutex_lock(&pyra
->pyra_lock
);
449 difference
= memcmp(buf
, &pyra
->settings
, sizeof(struct pyra_settings
));
451 retval
= pyra_set_settings(usb_dev
,
452 (struct pyra_settings
const *)buf
);
454 memcpy(&pyra
->settings
, buf
,
455 sizeof(struct pyra_settings
));
457 mutex_unlock(&pyra
->pyra_lock
);
462 profile_activated(pyra
, pyra
->settings
.startup_profile
);
464 return sizeof(struct pyra_settings
);
468 static ssize_t
pyra_sysfs_show_actual_cpi(struct device
*dev
,
469 struct device_attribute
*attr
, char *buf
)
471 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
472 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->actual_cpi
);
475 static ssize_t
pyra_sysfs_show_actual_profile(struct device
*dev
,
476 struct device_attribute
*attr
, char *buf
)
478 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
479 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->actual_profile
);
482 static ssize_t
pyra_sysfs_show_firmware_version(struct device
*dev
,
483 struct device_attribute
*attr
, char *buf
)
485 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
486 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->firmware_version
);
489 static ssize_t
pyra_sysfs_show_startup_profile(struct device
*dev
,
490 struct device_attribute
*attr
, char *buf
)
492 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
493 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->settings
.startup_profile
);
496 static DEVICE_ATTR(actual_cpi
, 0440, pyra_sysfs_show_actual_cpi
, NULL
);
498 static DEVICE_ATTR(actual_profile
, 0440, pyra_sysfs_show_actual_profile
, NULL
);
500 static DEVICE_ATTR(firmware_version
, 0440,
501 pyra_sysfs_show_firmware_version
, NULL
);
503 static DEVICE_ATTR(startup_profile
, 0440,
504 pyra_sysfs_show_startup_profile
, NULL
);
506 static struct attribute
*pyra_attributes
[] = {
507 &dev_attr_actual_cpi
.attr
,
508 &dev_attr_actual_profile
.attr
,
509 &dev_attr_firmware_version
.attr
,
510 &dev_attr_startup_profile
.attr
,
514 static struct attribute_group pyra_attribute_group
= {
515 .attrs
= pyra_attributes
518 static struct bin_attribute pyra_profile_settings_attr
= {
519 .attr
= { .name
= "profile_settings", .mode
= 0220 },
520 .size
= sizeof(struct pyra_profile_settings
),
521 .write
= pyra_sysfs_write_profile_settings
524 static struct bin_attribute pyra_profile1_settings_attr
= {
525 .attr
= { .name
= "profile1_settings", .mode
= 0440 },
526 .size
= sizeof(struct pyra_profile_settings
),
527 .read
= pyra_sysfs_read_profile1_settings
530 static struct bin_attribute pyra_profile2_settings_attr
= {
531 .attr
= { .name
= "profile2_settings", .mode
= 0440 },
532 .size
= sizeof(struct pyra_profile_settings
),
533 .read
= pyra_sysfs_read_profile2_settings
536 static struct bin_attribute pyra_profile3_settings_attr
= {
537 .attr
= { .name
= "profile3_settings", .mode
= 0440 },
538 .size
= sizeof(struct pyra_profile_settings
),
539 .read
= pyra_sysfs_read_profile3_settings
542 static struct bin_attribute pyra_profile4_settings_attr
= {
543 .attr
= { .name
= "profile4_settings", .mode
= 0440 },
544 .size
= sizeof(struct pyra_profile_settings
),
545 .read
= pyra_sysfs_read_profile4_settings
548 static struct bin_attribute pyra_profile5_settings_attr
= {
549 .attr
= { .name
= "profile5_settings", .mode
= 0440 },
550 .size
= sizeof(struct pyra_profile_settings
),
551 .read
= pyra_sysfs_read_profile5_settings
554 static struct bin_attribute pyra_profile_buttons_attr
= {
555 .attr
= { .name
= "profile_buttons", .mode
= 0220 },
556 .size
= sizeof(struct pyra_profile_buttons
),
557 .write
= pyra_sysfs_write_profile_buttons
560 static struct bin_attribute pyra_profile1_buttons_attr
= {
561 .attr
= { .name
= "profile1_buttons", .mode
= 0440 },
562 .size
= sizeof(struct pyra_profile_buttons
),
563 .read
= pyra_sysfs_read_profile1_buttons
566 static struct bin_attribute pyra_profile2_buttons_attr
= {
567 .attr
= { .name
= "profile2_buttons", .mode
= 0440 },
568 .size
= sizeof(struct pyra_profile_buttons
),
569 .read
= pyra_sysfs_read_profile2_buttons
572 static struct bin_attribute pyra_profile3_buttons_attr
= {
573 .attr
= { .name
= "profile3_buttons", .mode
= 0440 },
574 .size
= sizeof(struct pyra_profile_buttons
),
575 .read
= pyra_sysfs_read_profile3_buttons
578 static struct bin_attribute pyra_profile4_buttons_attr
= {
579 .attr
= { .name
= "profile4_buttons", .mode
= 0440 },
580 .size
= sizeof(struct pyra_profile_buttons
),
581 .read
= pyra_sysfs_read_profile4_buttons
584 static struct bin_attribute pyra_profile5_buttons_attr
= {
585 .attr
= { .name
= "profile5_buttons", .mode
= 0440 },
586 .size
= sizeof(struct pyra_profile_buttons
),
587 .read
= pyra_sysfs_read_profile5_buttons
590 static struct bin_attribute pyra_settings_attr
= {
591 .attr
= { .name
= "settings", .mode
= 0660 },
592 .size
= sizeof(struct pyra_settings
),
593 .read
= pyra_sysfs_read_settings
,
594 .write
= pyra_sysfs_write_settings
597 static int pyra_create_sysfs_attributes(struct usb_interface
*intf
)
601 retval
= sysfs_create_group(&intf
->dev
.kobj
, &pyra_attribute_group
);
605 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
606 &pyra_profile_settings_attr
);
610 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
611 &pyra_profile1_settings_attr
);
615 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
616 &pyra_profile2_settings_attr
);
620 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
621 &pyra_profile3_settings_attr
);
625 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
626 &pyra_profile4_settings_attr
);
630 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
631 &pyra_profile5_settings_attr
);
635 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
636 &pyra_profile_buttons_attr
);
640 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
641 &pyra_profile1_buttons_attr
);
645 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
646 &pyra_profile2_buttons_attr
);
650 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
651 &pyra_profile3_buttons_attr
);
655 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
656 &pyra_profile4_buttons_attr
);
660 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
661 &pyra_profile5_buttons_attr
);
665 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
666 &pyra_settings_attr
);
673 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile5_buttons_attr
);
675 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile4_buttons_attr
);
677 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile3_buttons_attr
);
679 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile2_buttons_attr
);
681 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile1_buttons_attr
);
683 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile_buttons_attr
);
685 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile5_settings_attr
);
687 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile4_settings_attr
);
689 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile3_settings_attr
);
691 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile2_settings_attr
);
693 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile1_settings_attr
);
695 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile_settings_attr
);
697 sysfs_remove_group(&intf
->dev
.kobj
, &pyra_attribute_group
);
702 static void pyra_remove_sysfs_attributes(struct usb_interface
*intf
)
704 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_settings_attr
);
705 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile5_buttons_attr
);
706 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile4_buttons_attr
);
707 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile3_buttons_attr
);
708 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile2_buttons_attr
);
709 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile1_buttons_attr
);
710 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile_buttons_attr
);
711 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile5_settings_attr
);
712 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile4_settings_attr
);
713 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile3_settings_attr
);
714 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile2_settings_attr
);
715 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile1_settings_attr
);
716 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile_settings_attr
);
717 sysfs_remove_group(&intf
->dev
.kobj
, &pyra_attribute_group
);
720 static int pyra_init_pyra_device_struct(struct usb_device
*usb_dev
,
721 struct pyra_device
*pyra
)
723 struct pyra_info
*info
;
726 mutex_init(&pyra
->pyra_lock
);
728 info
= kmalloc(sizeof(struct pyra_info
), GFP_KERNEL
);
731 retval
= pyra_get_info(usb_dev
, info
);
736 pyra
->firmware_version
= info
->firmware_version
;
739 retval
= pyra_get_settings(usb_dev
, &pyra
->settings
);
743 for (i
= 0; i
< 5; ++i
) {
744 retval
= pyra_get_profile_settings(usb_dev
,
745 &pyra
->profile_settings
[i
], i
);
749 retval
= pyra_get_profile_buttons(usb_dev
,
750 &pyra
->profile_buttons
[i
], i
);
755 profile_activated(pyra
, pyra
->settings
.startup_profile
);
760 static int pyra_init_specials(struct hid_device
*hdev
)
762 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
763 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
764 struct pyra_device
*pyra
;
767 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
768 == USB_INTERFACE_PROTOCOL_MOUSE
) {
770 pyra
= kzalloc(sizeof(*pyra
), GFP_KERNEL
);
772 hid_err(hdev
, "can't alloc device descriptor\n");
775 hid_set_drvdata(hdev
, pyra
);
777 retval
= pyra_init_pyra_device_struct(usb_dev
, pyra
);
779 hid_err(hdev
, "couldn't init struct pyra_device\n");
783 retval
= roccat_connect(hdev
);
785 hid_err(hdev
, "couldn't init char dev\n");
787 pyra
->chrdev_minor
= retval
;
788 pyra
->roccat_claimed
= 1;
791 retval
= pyra_create_sysfs_attributes(intf
);
793 hid_err(hdev
, "cannot create sysfs files\n");
797 hid_set_drvdata(hdev
, NULL
);
806 static void pyra_remove_specials(struct hid_device
*hdev
)
808 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
809 struct pyra_device
*pyra
;
811 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
812 == USB_INTERFACE_PROTOCOL_MOUSE
) {
813 pyra_remove_sysfs_attributes(intf
);
814 pyra
= hid_get_drvdata(hdev
);
815 if (pyra
->roccat_claimed
)
816 roccat_disconnect(pyra
->chrdev_minor
);
817 kfree(hid_get_drvdata(hdev
));
821 static int pyra_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
825 retval
= hid_parse(hdev
);
827 hid_err(hdev
, "parse failed\n");
831 retval
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
833 hid_err(hdev
, "hw start failed\n");
837 retval
= pyra_init_specials(hdev
);
839 hid_err(hdev
, "couldn't install mouse\n");
850 static void pyra_remove(struct hid_device
*hdev
)
852 pyra_remove_specials(hdev
);
856 static void pyra_keep_values_up_to_date(struct pyra_device
*pyra
,
859 struct pyra_mouse_event_button
const *button_event
;
862 case PYRA_MOUSE_REPORT_NUMBER_BUTTON
:
863 button_event
= (struct pyra_mouse_event_button
const *)data
;
864 switch (button_event
->type
) {
865 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2
:
866 profile_activated(pyra
, button_event
->data1
- 1);
868 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI
:
869 pyra
->actual_cpi
= button_event
->data1
;
876 static void pyra_report_to_chrdev(struct pyra_device
const *pyra
,
879 struct pyra_roccat_report roccat_report
;
880 struct pyra_mouse_event_button
const *button_event
;
882 if (data
[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON
)
885 button_event
= (struct pyra_mouse_event_button
const *)data
;
887 switch (button_event
->type
) {
888 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2
:
889 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI
:
890 roccat_report
.type
= button_event
->type
;
891 roccat_report
.value
= button_event
->data1
;
892 roccat_report
.key
= 0;
893 roccat_report_event(pyra
->chrdev_minor
,
894 (uint8_t const *)&roccat_report
,
895 sizeof(struct pyra_roccat_report
));
897 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO
:
898 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT
:
899 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH
:
900 if (button_event
->data2
== PYRA_MOUSE_EVENT_BUTTON_PRESS
) {
901 roccat_report
.type
= button_event
->type
;
902 roccat_report
.key
= button_event
->data1
;
904 * pyra reports profile numbers with range 1-5.
905 * Keeping this behaviour.
907 roccat_report
.value
= pyra
->actual_profile
+ 1;
908 roccat_report_event(pyra
->chrdev_minor
,
909 (uint8_t const *)&roccat_report
,
910 sizeof(struct pyra_roccat_report
));
916 static int pyra_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
919 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
920 struct pyra_device
*pyra
= hid_get_drvdata(hdev
);
922 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
923 != USB_INTERFACE_PROTOCOL_MOUSE
)
926 pyra_keep_values_up_to_date(pyra
, data
);
928 if (pyra
->roccat_claimed
)
929 pyra_report_to_chrdev(pyra
, data
);
934 static const struct hid_device_id pyra_devices
[] = {
935 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
,
936 USB_DEVICE_ID_ROCCAT_PYRA_WIRED
) },
937 /* TODO add USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS after testing */
941 MODULE_DEVICE_TABLE(hid
, pyra_devices
);
943 static struct hid_driver pyra_driver
= {
945 .id_table
= pyra_devices
,
947 .remove
= pyra_remove
,
948 .raw_event
= pyra_raw_event
951 static int __init
pyra_init(void)
953 return hid_register_driver(&pyra_driver
);
956 static void __exit
pyra_exit(void)
958 hid_unregister_driver(&pyra_driver
);
961 module_init(pyra_init
);
962 module_exit(pyra_exit
);
964 MODULE_AUTHOR("Stefan Achatz");
965 MODULE_DESCRIPTION("USB Roccat Pyra driver");
966 MODULE_LICENSE("GPL v2");