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 dev_err(&usb_dev
->dev
, "receive control status: "
91 "unknown response 0x%x 0x%x\n",
92 control
.request
, control
.value
);
97 static int pyra_get_profile_settings(struct usb_device
*usb_dev
,
98 struct pyra_profile_settings
*buf
, int number
)
102 retval
= pyra_send_control(usb_dev
, number
,
103 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS
);
108 retval
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
109 USB_REQ_CLEAR_FEATURE
,
110 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
111 PYRA_USB_COMMAND_PROFILE_SETTINGS
, 0, (char *)buf
,
112 sizeof(struct pyra_profile_settings
),
113 USB_CTRL_SET_TIMEOUT
);
115 if (retval
!= sizeof(struct pyra_profile_settings
))
121 static int pyra_get_profile_buttons(struct usb_device
*usb_dev
,
122 struct pyra_profile_buttons
*buf
, int number
)
126 retval
= pyra_send_control(usb_dev
, number
,
127 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS
);
132 retval
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
133 USB_REQ_CLEAR_FEATURE
,
134 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
135 PYRA_USB_COMMAND_PROFILE_BUTTONS
, 0, (char *)buf
,
136 sizeof(struct pyra_profile_buttons
),
137 USB_CTRL_SET_TIMEOUT
);
139 if (retval
!= sizeof(struct pyra_profile_buttons
))
145 static int pyra_get_settings(struct usb_device
*usb_dev
,
146 struct pyra_settings
*buf
)
149 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
150 USB_REQ_CLEAR_FEATURE
,
151 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
152 PYRA_USB_COMMAND_SETTINGS
, 0, buf
,
153 sizeof(struct pyra_settings
), USB_CTRL_SET_TIMEOUT
);
154 if (len
!= sizeof(struct pyra_settings
))
159 static int pyra_get_info(struct usb_device
*usb_dev
, struct pyra_info
*buf
)
162 len
= usb_control_msg(usb_dev
, usb_rcvctrlpipe(usb_dev
, 0),
163 USB_REQ_CLEAR_FEATURE
,
164 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_IN
,
165 PYRA_USB_COMMAND_INFO
, 0, buf
,
166 sizeof(struct pyra_info
), USB_CTRL_SET_TIMEOUT
);
167 if (len
!= sizeof(struct pyra_info
))
172 static int pyra_set_profile_settings(struct usb_device
*usb_dev
,
173 struct pyra_profile_settings
const *settings
)
176 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
177 USB_REQ_SET_CONFIGURATION
,
178 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
179 PYRA_USB_COMMAND_PROFILE_SETTINGS
, 0, (char *)settings
,
180 sizeof(struct pyra_profile_settings
),
181 USB_CTRL_SET_TIMEOUT
);
182 if (len
!= sizeof(struct pyra_profile_settings
))
184 if (pyra_receive_control_status(usb_dev
))
189 static int pyra_set_profile_buttons(struct usb_device
*usb_dev
,
190 struct pyra_profile_buttons
const *buttons
)
193 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
194 USB_REQ_SET_CONFIGURATION
,
195 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
196 PYRA_USB_COMMAND_PROFILE_BUTTONS
, 0, (char *)buttons
,
197 sizeof(struct pyra_profile_buttons
),
198 USB_CTRL_SET_TIMEOUT
);
199 if (len
!= sizeof(struct pyra_profile_buttons
))
201 if (pyra_receive_control_status(usb_dev
))
206 static int pyra_set_settings(struct usb_device
*usb_dev
,
207 struct pyra_settings
const *settings
)
210 len
= usb_control_msg(usb_dev
, usb_sndctrlpipe(usb_dev
, 0),
211 USB_REQ_SET_CONFIGURATION
,
212 USB_TYPE_CLASS
| USB_RECIP_INTERFACE
| USB_DIR_OUT
,
213 PYRA_USB_COMMAND_SETTINGS
, 0, (char *)settings
,
214 sizeof(struct pyra_settings
), USB_CTRL_SET_TIMEOUT
);
215 if (len
!= sizeof(struct pyra_settings
))
217 if (pyra_receive_control_status(usb_dev
))
222 static ssize_t
pyra_sysfs_read_profilex_settings(struct file
*fp
,
223 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
224 loff_t off
, size_t count
, int number
)
226 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
227 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
229 if (off
>= sizeof(struct pyra_profile_settings
))
232 if (off
+ count
> sizeof(struct pyra_profile_settings
))
233 count
= sizeof(struct pyra_profile_settings
) - off
;
235 mutex_lock(&pyra
->pyra_lock
);
236 memcpy(buf
, ((char const *)&pyra
->profile_settings
[number
]) + off
,
238 mutex_unlock(&pyra
->pyra_lock
);
243 static ssize_t
pyra_sysfs_read_profile1_settings(struct file
*fp
,
244 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
245 loff_t off
, size_t count
)
247 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
248 attr
, buf
, off
, count
, 0);
251 static ssize_t
pyra_sysfs_read_profile2_settings(struct file
*fp
,
252 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
253 loff_t off
, size_t count
)
255 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
256 attr
, buf
, off
, count
, 1);
259 static ssize_t
pyra_sysfs_read_profile3_settings(struct file
*fp
,
260 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
261 loff_t off
, size_t count
)
263 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
264 attr
, buf
, off
, count
, 2);
267 static ssize_t
pyra_sysfs_read_profile4_settings(struct file
*fp
,
268 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
269 loff_t off
, size_t count
)
271 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
272 attr
, buf
, off
, count
, 3);
275 static ssize_t
pyra_sysfs_read_profile5_settings(struct file
*fp
,
276 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
277 loff_t off
, size_t count
)
279 return pyra_sysfs_read_profilex_settings(fp
, kobj
,
280 attr
, buf
, off
, count
, 4);
283 static ssize_t
pyra_sysfs_read_profilex_buttons(struct file
*fp
,
284 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
285 loff_t off
, size_t count
, int number
)
287 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
288 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
290 if (off
>= sizeof(struct pyra_profile_buttons
))
293 if (off
+ count
> sizeof(struct pyra_profile_buttons
))
294 count
= sizeof(struct pyra_profile_buttons
) - off
;
296 mutex_lock(&pyra
->pyra_lock
);
297 memcpy(buf
, ((char const *)&pyra
->profile_buttons
[number
]) + off
,
299 mutex_unlock(&pyra
->pyra_lock
);
304 static ssize_t
pyra_sysfs_read_profile1_buttons(struct file
*fp
,
305 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
306 loff_t off
, size_t count
)
308 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
309 attr
, buf
, off
, count
, 0);
312 static ssize_t
pyra_sysfs_read_profile2_buttons(struct file
*fp
,
313 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
314 loff_t off
, size_t count
)
316 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
317 attr
, buf
, off
, count
, 1);
320 static ssize_t
pyra_sysfs_read_profile3_buttons(struct file
*fp
,
321 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
322 loff_t off
, size_t count
)
324 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
325 attr
, buf
, off
, count
, 2);
328 static ssize_t
pyra_sysfs_read_profile4_buttons(struct file
*fp
,
329 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
330 loff_t off
, size_t count
)
332 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
333 attr
, buf
, off
, count
, 3);
336 static ssize_t
pyra_sysfs_read_profile5_buttons(struct file
*fp
,
337 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
338 loff_t off
, size_t count
)
340 return pyra_sysfs_read_profilex_buttons(fp
, kobj
,
341 attr
, buf
, off
, count
, 4);
344 static ssize_t
pyra_sysfs_write_profile_settings(struct file
*fp
,
345 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
346 loff_t off
, size_t count
)
348 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
349 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
350 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
354 struct pyra_profile_settings
*profile_settings
;
356 if (off
!= 0 || count
!= sizeof(struct pyra_profile_settings
))
359 profile_number
= ((struct pyra_profile_settings
const *)buf
)->number
;
360 profile_settings
= &pyra
->profile_settings
[profile_number
];
362 mutex_lock(&pyra
->pyra_lock
);
363 difference
= memcmp(buf
, profile_settings
,
364 sizeof(struct pyra_profile_settings
));
366 retval
= pyra_set_profile_settings(usb_dev
,
367 (struct pyra_profile_settings
const *)buf
);
369 memcpy(profile_settings
, buf
,
370 sizeof(struct pyra_profile_settings
));
372 mutex_unlock(&pyra
->pyra_lock
);
377 return sizeof(struct pyra_profile_settings
);
380 static ssize_t
pyra_sysfs_write_profile_buttons(struct file
*fp
,
381 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
382 loff_t off
, size_t count
)
384 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
385 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
386 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
390 struct pyra_profile_buttons
*profile_buttons
;
392 if (off
!= 0 || count
!= sizeof(struct pyra_profile_buttons
))
395 profile_number
= ((struct pyra_profile_buttons
const *)buf
)->number
;
396 profile_buttons
= &pyra
->profile_buttons
[profile_number
];
398 mutex_lock(&pyra
->pyra_lock
);
399 difference
= memcmp(buf
, profile_buttons
,
400 sizeof(struct pyra_profile_buttons
));
402 retval
= pyra_set_profile_buttons(usb_dev
,
403 (struct pyra_profile_buttons
const *)buf
);
405 memcpy(profile_buttons
, buf
,
406 sizeof(struct pyra_profile_buttons
));
408 mutex_unlock(&pyra
->pyra_lock
);
413 return sizeof(struct pyra_profile_buttons
);
416 static ssize_t
pyra_sysfs_read_settings(struct file
*fp
,
417 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
418 loff_t off
, size_t count
)
420 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
421 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
423 if (off
>= sizeof(struct pyra_settings
))
426 if (off
+ count
> sizeof(struct pyra_settings
))
427 count
= sizeof(struct pyra_settings
) - off
;
429 mutex_lock(&pyra
->pyra_lock
);
430 memcpy(buf
, ((char const *)&pyra
->settings
) + off
, count
);
431 mutex_unlock(&pyra
->pyra_lock
);
436 static ssize_t
pyra_sysfs_write_settings(struct file
*fp
,
437 struct kobject
*kobj
, struct bin_attribute
*attr
, char *buf
,
438 loff_t off
, size_t count
)
440 struct device
*dev
= container_of(kobj
, struct device
, kobj
);
441 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
442 struct usb_device
*usb_dev
= interface_to_usbdev(to_usb_interface(dev
));
446 if (off
!= 0 || count
!= sizeof(struct pyra_settings
))
449 mutex_lock(&pyra
->pyra_lock
);
450 difference
= memcmp(buf
, &pyra
->settings
, sizeof(struct pyra_settings
));
452 retval
= pyra_set_settings(usb_dev
,
453 (struct pyra_settings
const *)buf
);
455 memcpy(&pyra
->settings
, buf
,
456 sizeof(struct pyra_settings
));
458 mutex_unlock(&pyra
->pyra_lock
);
463 profile_activated(pyra
, pyra
->settings
.startup_profile
);
465 return sizeof(struct pyra_settings
);
469 static ssize_t
pyra_sysfs_show_actual_cpi(struct device
*dev
,
470 struct device_attribute
*attr
, char *buf
)
472 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
473 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->actual_cpi
);
476 static ssize_t
pyra_sysfs_show_actual_profile(struct device
*dev
,
477 struct device_attribute
*attr
, char *buf
)
479 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
480 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->actual_profile
);
483 static ssize_t
pyra_sysfs_show_firmware_version(struct device
*dev
,
484 struct device_attribute
*attr
, char *buf
)
486 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
487 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->firmware_version
);
490 static ssize_t
pyra_sysfs_show_startup_profile(struct device
*dev
,
491 struct device_attribute
*attr
, char *buf
)
493 struct pyra_device
*pyra
= hid_get_drvdata(dev_get_drvdata(dev
));
494 return snprintf(buf
, PAGE_SIZE
, "%d\n", pyra
->settings
.startup_profile
);
497 static DEVICE_ATTR(actual_cpi
, 0440, pyra_sysfs_show_actual_cpi
, NULL
);
499 static DEVICE_ATTR(actual_profile
, 0440, pyra_sysfs_show_actual_profile
, NULL
);
501 static DEVICE_ATTR(firmware_version
, 0440,
502 pyra_sysfs_show_firmware_version
, NULL
);
504 static DEVICE_ATTR(startup_profile
, 0440,
505 pyra_sysfs_show_startup_profile
, NULL
);
507 static struct attribute
*pyra_attributes
[] = {
508 &dev_attr_actual_cpi
.attr
,
509 &dev_attr_actual_profile
.attr
,
510 &dev_attr_firmware_version
.attr
,
511 &dev_attr_startup_profile
.attr
,
515 static struct attribute_group pyra_attribute_group
= {
516 .attrs
= pyra_attributes
519 static struct bin_attribute pyra_profile_settings_attr
= {
520 .attr
= { .name
= "profile_settings", .mode
= 0220 },
521 .size
= sizeof(struct pyra_profile_settings
),
522 .write
= pyra_sysfs_write_profile_settings
525 static struct bin_attribute pyra_profile1_settings_attr
= {
526 .attr
= { .name
= "profile1_settings", .mode
= 0440 },
527 .size
= sizeof(struct pyra_profile_settings
),
528 .read
= pyra_sysfs_read_profile1_settings
531 static struct bin_attribute pyra_profile2_settings_attr
= {
532 .attr
= { .name
= "profile2_settings", .mode
= 0440 },
533 .size
= sizeof(struct pyra_profile_settings
),
534 .read
= pyra_sysfs_read_profile2_settings
537 static struct bin_attribute pyra_profile3_settings_attr
= {
538 .attr
= { .name
= "profile3_settings", .mode
= 0440 },
539 .size
= sizeof(struct pyra_profile_settings
),
540 .read
= pyra_sysfs_read_profile3_settings
543 static struct bin_attribute pyra_profile4_settings_attr
= {
544 .attr
= { .name
= "profile4_settings", .mode
= 0440 },
545 .size
= sizeof(struct pyra_profile_settings
),
546 .read
= pyra_sysfs_read_profile4_settings
549 static struct bin_attribute pyra_profile5_settings_attr
= {
550 .attr
= { .name
= "profile5_settings", .mode
= 0440 },
551 .size
= sizeof(struct pyra_profile_settings
),
552 .read
= pyra_sysfs_read_profile5_settings
555 static struct bin_attribute pyra_profile_buttons_attr
= {
556 .attr
= { .name
= "profile_buttons", .mode
= 0220 },
557 .size
= sizeof(struct pyra_profile_buttons
),
558 .write
= pyra_sysfs_write_profile_buttons
561 static struct bin_attribute pyra_profile1_buttons_attr
= {
562 .attr
= { .name
= "profile1_buttons", .mode
= 0440 },
563 .size
= sizeof(struct pyra_profile_buttons
),
564 .read
= pyra_sysfs_read_profile1_buttons
567 static struct bin_attribute pyra_profile2_buttons_attr
= {
568 .attr
= { .name
= "profile2_buttons", .mode
= 0440 },
569 .size
= sizeof(struct pyra_profile_buttons
),
570 .read
= pyra_sysfs_read_profile2_buttons
573 static struct bin_attribute pyra_profile3_buttons_attr
= {
574 .attr
= { .name
= "profile3_buttons", .mode
= 0440 },
575 .size
= sizeof(struct pyra_profile_buttons
),
576 .read
= pyra_sysfs_read_profile3_buttons
579 static struct bin_attribute pyra_profile4_buttons_attr
= {
580 .attr
= { .name
= "profile4_buttons", .mode
= 0440 },
581 .size
= sizeof(struct pyra_profile_buttons
),
582 .read
= pyra_sysfs_read_profile4_buttons
585 static struct bin_attribute pyra_profile5_buttons_attr
= {
586 .attr
= { .name
= "profile5_buttons", .mode
= 0440 },
587 .size
= sizeof(struct pyra_profile_buttons
),
588 .read
= pyra_sysfs_read_profile5_buttons
591 static struct bin_attribute pyra_settings_attr
= {
592 .attr
= { .name
= "settings", .mode
= 0660 },
593 .size
= sizeof(struct pyra_settings
),
594 .read
= pyra_sysfs_read_settings
,
595 .write
= pyra_sysfs_write_settings
598 static int pyra_create_sysfs_attributes(struct usb_interface
*intf
)
602 retval
= sysfs_create_group(&intf
->dev
.kobj
, &pyra_attribute_group
);
606 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
607 &pyra_profile_settings_attr
);
611 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
612 &pyra_profile1_settings_attr
);
616 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
617 &pyra_profile2_settings_attr
);
621 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
622 &pyra_profile3_settings_attr
);
626 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
627 &pyra_profile4_settings_attr
);
631 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
632 &pyra_profile5_settings_attr
);
636 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
637 &pyra_profile_buttons_attr
);
641 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
642 &pyra_profile1_buttons_attr
);
646 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
647 &pyra_profile2_buttons_attr
);
651 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
652 &pyra_profile3_buttons_attr
);
656 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
657 &pyra_profile4_buttons_attr
);
661 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
662 &pyra_profile5_buttons_attr
);
666 retval
= sysfs_create_bin_file(&intf
->dev
.kobj
,
667 &pyra_settings_attr
);
674 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile5_buttons_attr
);
676 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile4_buttons_attr
);
678 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile3_buttons_attr
);
680 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile2_buttons_attr
);
682 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile1_buttons_attr
);
684 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile_buttons_attr
);
686 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile5_settings_attr
);
688 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile4_settings_attr
);
690 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile3_settings_attr
);
692 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile2_settings_attr
);
694 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile1_settings_attr
);
696 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile_settings_attr
);
698 sysfs_remove_group(&intf
->dev
.kobj
, &pyra_attribute_group
);
703 static void pyra_remove_sysfs_attributes(struct usb_interface
*intf
)
705 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_settings_attr
);
706 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile5_buttons_attr
);
707 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile4_buttons_attr
);
708 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile3_buttons_attr
);
709 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile2_buttons_attr
);
710 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile1_buttons_attr
);
711 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile_buttons_attr
);
712 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile5_settings_attr
);
713 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile4_settings_attr
);
714 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile3_settings_attr
);
715 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile2_settings_attr
);
716 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile1_settings_attr
);
717 sysfs_remove_bin_file(&intf
->dev
.kobj
, &pyra_profile_settings_attr
);
718 sysfs_remove_group(&intf
->dev
.kobj
, &pyra_attribute_group
);
721 static int pyra_init_pyra_device_struct(struct usb_device
*usb_dev
,
722 struct pyra_device
*pyra
)
724 struct pyra_info
*info
;
727 mutex_init(&pyra
->pyra_lock
);
729 info
= kmalloc(sizeof(struct pyra_info
), GFP_KERNEL
);
732 retval
= pyra_get_info(usb_dev
, info
);
737 pyra
->firmware_version
= info
->firmware_version
;
740 retval
= pyra_get_settings(usb_dev
, &pyra
->settings
);
744 for (i
= 0; i
< 5; ++i
) {
745 retval
= pyra_get_profile_settings(usb_dev
,
746 &pyra
->profile_settings
[i
], i
);
750 retval
= pyra_get_profile_buttons(usb_dev
,
751 &pyra
->profile_buttons
[i
], i
);
756 profile_activated(pyra
, pyra
->settings
.startup_profile
);
761 static int pyra_init_specials(struct hid_device
*hdev
)
763 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
764 struct usb_device
*usb_dev
= interface_to_usbdev(intf
);
765 struct pyra_device
*pyra
;
768 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
769 == USB_INTERFACE_PROTOCOL_MOUSE
) {
771 pyra
= kzalloc(sizeof(*pyra
), GFP_KERNEL
);
773 dev_err(&hdev
->dev
, "can't alloc device descriptor\n");
776 hid_set_drvdata(hdev
, pyra
);
778 retval
= pyra_init_pyra_device_struct(usb_dev
, pyra
);
781 "couldn't init struct pyra_device\n");
785 retval
= roccat_connect(hdev
);
787 dev_err(&hdev
->dev
, "couldn't init char dev\n");
789 pyra
->chrdev_minor
= retval
;
790 pyra
->roccat_claimed
= 1;
793 retval
= pyra_create_sysfs_attributes(intf
);
795 dev_err(&hdev
->dev
, "cannot create sysfs files\n");
799 hid_set_drvdata(hdev
, NULL
);
808 static void pyra_remove_specials(struct hid_device
*hdev
)
810 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
811 struct pyra_device
*pyra
;
813 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
814 == USB_INTERFACE_PROTOCOL_MOUSE
) {
815 pyra_remove_sysfs_attributes(intf
);
816 pyra
= hid_get_drvdata(hdev
);
817 if (pyra
->roccat_claimed
)
818 roccat_disconnect(pyra
->chrdev_minor
);
819 kfree(hid_get_drvdata(hdev
));
823 static int pyra_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
827 retval
= hid_parse(hdev
);
829 dev_err(&hdev
->dev
, "parse failed\n");
833 retval
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
);
835 dev_err(&hdev
->dev
, "hw start failed\n");
839 retval
= pyra_init_specials(hdev
);
841 dev_err(&hdev
->dev
, "couldn't install mouse\n");
852 static void pyra_remove(struct hid_device
*hdev
)
854 pyra_remove_specials(hdev
);
858 static void pyra_keep_values_up_to_date(struct pyra_device
*pyra
,
861 struct pyra_mouse_event_button
const *button_event
;
864 case PYRA_MOUSE_REPORT_NUMBER_BUTTON
:
865 button_event
= (struct pyra_mouse_event_button
const *)data
;
866 switch (button_event
->type
) {
867 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2
:
868 profile_activated(pyra
, button_event
->data1
- 1);
870 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI
:
871 pyra
->actual_cpi
= button_event
->data1
;
878 static void pyra_report_to_chrdev(struct pyra_device
const *pyra
,
881 struct pyra_roccat_report roccat_report
;
882 struct pyra_mouse_event_button
const *button_event
;
884 if (data
[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON
)
887 button_event
= (struct pyra_mouse_event_button
const *)data
;
889 switch (button_event
->type
) {
890 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2
:
891 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI
:
892 roccat_report
.type
= button_event
->type
;
893 roccat_report
.value
= button_event
->data1
;
894 roccat_report
.key
= 0;
895 roccat_report_event(pyra
->chrdev_minor
,
896 (uint8_t const *)&roccat_report
,
897 sizeof(struct pyra_roccat_report
));
899 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO
:
900 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT
:
901 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH
:
902 if (button_event
->data2
== PYRA_MOUSE_EVENT_BUTTON_PRESS
) {
903 roccat_report
.type
= button_event
->type
;
904 roccat_report
.key
= button_event
->data1
;
906 * pyra reports profile numbers with range 1-5.
907 * Keeping this behaviour.
909 roccat_report
.value
= pyra
->actual_profile
+ 1;
910 roccat_report_event(pyra
->chrdev_minor
,
911 (uint8_t const *)&roccat_report
,
912 sizeof(struct pyra_roccat_report
));
918 static int pyra_raw_event(struct hid_device
*hdev
, struct hid_report
*report
,
921 struct usb_interface
*intf
= to_usb_interface(hdev
->dev
.parent
);
922 struct pyra_device
*pyra
= hid_get_drvdata(hdev
);
924 if (intf
->cur_altsetting
->desc
.bInterfaceProtocol
925 != USB_INTERFACE_PROTOCOL_MOUSE
)
928 pyra_keep_values_up_to_date(pyra
, data
);
930 if (pyra
->roccat_claimed
)
931 pyra_report_to_chrdev(pyra
, data
);
936 static const struct hid_device_id pyra_devices
[] = {
937 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT
,
938 USB_DEVICE_ID_ROCCAT_PYRA_WIRED
) },
939 /* TODO add USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS after testing */
943 MODULE_DEVICE_TABLE(hid
, pyra_devices
);
945 static struct hid_driver pyra_driver
= {
947 .id_table
= pyra_devices
,
949 .remove
= pyra_remove
,
950 .raw_event
= pyra_raw_event
953 static int __init
pyra_init(void)
955 return hid_register_driver(&pyra_driver
);
958 static void __exit
pyra_exit(void)
960 hid_unregister_driver(&pyra_driver
);
963 module_init(pyra_init
);
964 module_exit(pyra_exit
);
966 MODULE_AUTHOR("Stefan Achatz");
967 MODULE_DESCRIPTION("USB Roccat Pyra driver");
968 MODULE_LICENSE("GPL v2");