xref: /linux/drivers/hid/hid-roccat-pyra.c (revision 5012aada506cb8b570e46579077c0ec5b82ebd5d)
1 /*
2  * Roccat Pyra driver for Linux
3  *
4  * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
5  */
6 
7 /*
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)
11  * any later version.
12  */
13 
14 /*
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
18  */
19 
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>
26 #include "hid-ids.h"
27 #include "hid-roccat.h"
28 #include "hid-roccat-pyra.h"
29 
30 /* pyra_class is used for creating sysfs attributes via roccat char device */
31 static struct class *pyra_class;
32 
33 static void profile_activated(struct pyra_device *pyra,
34 		unsigned int new_profile)
35 {
36 	pyra->actual_profile = new_profile;
37 	pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
38 }
39 
40 static int pyra_send_control(struct usb_device *usb_dev, int value,
41 		enum pyra_control_requests request)
42 {
43 	int len;
44 	struct pyra_control control;
45 
46 	if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
47 			request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
48 			(value < 0 || value > 4))
49 		return -EINVAL;
50 
51 	control.command = PYRA_COMMAND_CONTROL;
52 	control.value = value;
53 	control.request = request;
54 
55 	len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
56 			USB_REQ_SET_CONFIGURATION,
57 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
58 			PYRA_USB_COMMAND_CONTROL, 0, (char *)&control,
59 			sizeof(struct pyra_control),
60 			USB_CTRL_SET_TIMEOUT);
61 
62 	if (len != sizeof(struct pyra_control))
63 		return len;
64 
65 	return 0;
66 }
67 
68 static int pyra_receive_control_status(struct usb_device *usb_dev)
69 {
70 	int len;
71 	struct pyra_control control;
72 
73 	do {
74 		msleep(10);
75 
76 		len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
77 				USB_REQ_CLEAR_FEATURE,
78 				USB_TYPE_CLASS | USB_RECIP_INTERFACE |
79 				USB_DIR_IN,
80 				PYRA_USB_COMMAND_CONTROL, 0, (char *)&control,
81 				sizeof(struct pyra_control),
82 				USB_CTRL_SET_TIMEOUT);
83 
84 		/* requested too early, try again */
85 	} while (len == -EPROTO);
86 
87 	if (len == sizeof(struct pyra_control) &&
88 			control.command == PYRA_COMMAND_CONTROL &&
89 			control.request == PYRA_CONTROL_REQUEST_STATUS &&
90 			control.value == 1)
91 			return 0;
92 	else {
93 		hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n",
94 			control.request, control.value);
95 		return -EINVAL;
96 	}
97 }
98 
99 static int pyra_get_profile_settings(struct usb_device *usb_dev,
100 		struct pyra_profile_settings *buf, int number)
101 {
102 	int retval;
103 
104 	retval = pyra_send_control(usb_dev, number,
105 			PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
106 
107 	if (retval)
108 		return retval;
109 
110 	retval = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
111 			USB_REQ_CLEAR_FEATURE,
112 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
113 			PYRA_USB_COMMAND_PROFILE_SETTINGS, 0, (char *)buf,
114 			sizeof(struct pyra_profile_settings),
115 			USB_CTRL_SET_TIMEOUT);
116 
117 	if (retval != sizeof(struct pyra_profile_settings))
118 		return retval;
119 
120 	return 0;
121 }
122 
123 static int pyra_get_profile_buttons(struct usb_device *usb_dev,
124 		struct pyra_profile_buttons *buf, int number)
125 {
126 	int retval;
127 
128 	retval = pyra_send_control(usb_dev, number,
129 			PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
130 
131 	if (retval)
132 		return retval;
133 
134 	retval = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
135 			USB_REQ_CLEAR_FEATURE,
136 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
137 			PYRA_USB_COMMAND_PROFILE_BUTTONS, 0, (char *)buf,
138 			sizeof(struct pyra_profile_buttons),
139 			USB_CTRL_SET_TIMEOUT);
140 
141 	if (retval != sizeof(struct pyra_profile_buttons))
142 		return retval;
143 
144 	return 0;
145 }
146 
147 static int pyra_get_settings(struct usb_device *usb_dev,
148 		struct pyra_settings *buf)
149 {
150 	int len;
151 	len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
152 			USB_REQ_CLEAR_FEATURE,
153 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
154 			PYRA_USB_COMMAND_SETTINGS, 0, buf,
155 			sizeof(struct pyra_settings), USB_CTRL_SET_TIMEOUT);
156 	if (len != sizeof(struct pyra_settings))
157 		return -EIO;
158 	return 0;
159 }
160 
161 static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf)
162 {
163 	int len;
164 	len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
165 			USB_REQ_CLEAR_FEATURE,
166 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
167 			PYRA_USB_COMMAND_INFO, 0, buf,
168 			sizeof(struct pyra_info), USB_CTRL_SET_TIMEOUT);
169 	if (len != sizeof(struct pyra_info))
170 		return -EIO;
171 	return 0;
172 }
173 
174 static int pyra_set_profile_settings(struct usb_device *usb_dev,
175 		struct pyra_profile_settings const *settings)
176 {
177 	int len;
178 	len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
179 			USB_REQ_SET_CONFIGURATION,
180 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
181 			PYRA_USB_COMMAND_PROFILE_SETTINGS, 0, (char *)settings,
182 			sizeof(struct pyra_profile_settings),
183 			USB_CTRL_SET_TIMEOUT);
184 	if (len != sizeof(struct pyra_profile_settings))
185 		return -EIO;
186 	if (pyra_receive_control_status(usb_dev))
187 		return -EIO;
188 	return 0;
189 }
190 
191 static int pyra_set_profile_buttons(struct usb_device *usb_dev,
192 		struct pyra_profile_buttons const *buttons)
193 {
194 	int len;
195 	len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
196 			USB_REQ_SET_CONFIGURATION,
197 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
198 			PYRA_USB_COMMAND_PROFILE_BUTTONS, 0, (char *)buttons,
199 			sizeof(struct pyra_profile_buttons),
200 			USB_CTRL_SET_TIMEOUT);
201 	if (len != sizeof(struct pyra_profile_buttons))
202 		return -EIO;
203 	if (pyra_receive_control_status(usb_dev))
204 		return -EIO;
205 	return 0;
206 }
207 
208 static int pyra_set_settings(struct usb_device *usb_dev,
209 		struct pyra_settings const *settings)
210 {
211 	int len;
212 	len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
213 			USB_REQ_SET_CONFIGURATION,
214 			USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
215 			PYRA_USB_COMMAND_SETTINGS, 0, (char *)settings,
216 			sizeof(struct pyra_settings), USB_CTRL_SET_TIMEOUT);
217 	if (len != sizeof(struct pyra_settings))
218 		return -EIO;
219 	if (pyra_receive_control_status(usb_dev))
220 		return -EIO;
221 	return 0;
222 }
223 
224 static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
225 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
226 		loff_t off, size_t count, int number)
227 {
228 	struct device *dev =
229 			container_of(kobj, struct device, kobj)->parent->parent;
230 	struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
231 
232 	if (off >= sizeof(struct pyra_profile_settings))
233 		return 0;
234 
235 	if (off + count > sizeof(struct pyra_profile_settings))
236 		count = sizeof(struct pyra_profile_settings) - off;
237 
238 	mutex_lock(&pyra->pyra_lock);
239 	memcpy(buf, ((char const *)&pyra->profile_settings[number]) + off,
240 			count);
241 	mutex_unlock(&pyra->pyra_lock);
242 
243 	return count;
244 }
245 
246 static ssize_t pyra_sysfs_read_profile1_settings(struct file *fp,
247 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
248 		loff_t off, size_t count)
249 {
250 	return pyra_sysfs_read_profilex_settings(fp, kobj,
251 			attr, buf, off, count, 0);
252 }
253 
254 static ssize_t pyra_sysfs_read_profile2_settings(struct file *fp,
255 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
256 		loff_t off, size_t count)
257 {
258 	return pyra_sysfs_read_profilex_settings(fp, kobj,
259 			attr, buf, off, count, 1);
260 }
261 
262 static ssize_t pyra_sysfs_read_profile3_settings(struct file *fp,
263 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
264 		loff_t off, size_t count)
265 {
266 	return pyra_sysfs_read_profilex_settings(fp, kobj,
267 			attr, buf, off, count, 2);
268 }
269 
270 static ssize_t pyra_sysfs_read_profile4_settings(struct file *fp,
271 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
272 		loff_t off, size_t count)
273 {
274 	return pyra_sysfs_read_profilex_settings(fp, kobj,
275 			attr, buf, off, count, 3);
276 }
277 
278 static ssize_t pyra_sysfs_read_profile5_settings(struct file *fp,
279 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
280 		loff_t off, size_t count)
281 {
282 	return pyra_sysfs_read_profilex_settings(fp, kobj,
283 			attr, buf, off, count, 4);
284 }
285 
286 static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
287 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
288 		loff_t off, size_t count, int number)
289 {
290 	struct device *dev =
291 			container_of(kobj, struct device, kobj)->parent->parent;
292 	struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
293 
294 	if (off >= sizeof(struct pyra_profile_buttons))
295 		return 0;
296 
297 	if (off + count > sizeof(struct pyra_profile_buttons))
298 		count = sizeof(struct pyra_profile_buttons) - off;
299 
300 	mutex_lock(&pyra->pyra_lock);
301 	memcpy(buf, ((char const *)&pyra->profile_buttons[number]) + off,
302 			count);
303 	mutex_unlock(&pyra->pyra_lock);
304 
305 	return count;
306 }
307 
308 static ssize_t pyra_sysfs_read_profile1_buttons(struct file *fp,
309 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
310 		loff_t off, size_t count)
311 {
312 	return pyra_sysfs_read_profilex_buttons(fp, kobj,
313 			attr, buf, off, count, 0);
314 }
315 
316 static ssize_t pyra_sysfs_read_profile2_buttons(struct file *fp,
317 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
318 		loff_t off, size_t count)
319 {
320 	return pyra_sysfs_read_profilex_buttons(fp, kobj,
321 			attr, buf, off, count, 1);
322 }
323 
324 static ssize_t pyra_sysfs_read_profile3_buttons(struct file *fp,
325 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
326 		loff_t off, size_t count)
327 {
328 	return pyra_sysfs_read_profilex_buttons(fp, kobj,
329 			attr, buf, off, count, 2);
330 }
331 
332 static ssize_t pyra_sysfs_read_profile4_buttons(struct file *fp,
333 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
334 		loff_t off, size_t count)
335 {
336 	return pyra_sysfs_read_profilex_buttons(fp, kobj,
337 			attr, buf, off, count, 3);
338 }
339 
340 static ssize_t pyra_sysfs_read_profile5_buttons(struct file *fp,
341 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
342 		loff_t off, size_t count)
343 {
344 	return pyra_sysfs_read_profilex_buttons(fp, kobj,
345 			attr, buf, off, count, 4);
346 }
347 
348 static ssize_t pyra_sysfs_write_profile_settings(struct file *fp,
349 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
350 		loff_t off, size_t count)
351 {
352 	struct device *dev =
353 			container_of(kobj, struct device, kobj)->parent->parent;
354 	struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
355 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
356 	int retval = 0;
357 	int difference;
358 	int profile_number;
359 	struct pyra_profile_settings *profile_settings;
360 
361 	if (off != 0 || count != sizeof(struct pyra_profile_settings))
362 		return -EINVAL;
363 
364 	profile_number = ((struct pyra_profile_settings const *)buf)->number;
365 	profile_settings = &pyra->profile_settings[profile_number];
366 
367 	mutex_lock(&pyra->pyra_lock);
368 	difference = memcmp(buf, profile_settings,
369 			sizeof(struct pyra_profile_settings));
370 	if (difference) {
371 		retval = pyra_set_profile_settings(usb_dev,
372 				(struct pyra_profile_settings const *)buf);
373 		if (!retval)
374 			memcpy(profile_settings, buf,
375 					sizeof(struct pyra_profile_settings));
376 	}
377 	mutex_unlock(&pyra->pyra_lock);
378 
379 	if (retval)
380 		return retval;
381 
382 	return sizeof(struct pyra_profile_settings);
383 }
384 
385 static ssize_t pyra_sysfs_write_profile_buttons(struct file *fp,
386 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
387 		loff_t off, size_t count)
388 {
389 	struct device *dev =
390 			container_of(kobj, struct device, kobj)->parent->parent;
391 	struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
392 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
393 	int retval = 0;
394 	int difference;
395 	int profile_number;
396 	struct pyra_profile_buttons *profile_buttons;
397 
398 	if (off != 0 || count != sizeof(struct pyra_profile_buttons))
399 		return -EINVAL;
400 
401 	profile_number = ((struct pyra_profile_buttons const *)buf)->number;
402 	profile_buttons = &pyra->profile_buttons[profile_number];
403 
404 	mutex_lock(&pyra->pyra_lock);
405 	difference = memcmp(buf, profile_buttons,
406 			sizeof(struct pyra_profile_buttons));
407 	if (difference) {
408 		retval = pyra_set_profile_buttons(usb_dev,
409 				(struct pyra_profile_buttons const *)buf);
410 		if (!retval)
411 			memcpy(profile_buttons, buf,
412 					sizeof(struct pyra_profile_buttons));
413 	}
414 	mutex_unlock(&pyra->pyra_lock);
415 
416 	if (retval)
417 		return retval;
418 
419 	return sizeof(struct pyra_profile_buttons);
420 }
421 
422 static ssize_t pyra_sysfs_read_settings(struct file *fp,
423 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
424 		loff_t off, size_t count)
425 {
426 	struct device *dev =
427 			container_of(kobj, struct device, kobj)->parent->parent;
428 	struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
429 
430 	if (off >= sizeof(struct pyra_settings))
431 		return 0;
432 
433 	if (off + count > sizeof(struct pyra_settings))
434 		count = sizeof(struct pyra_settings) - off;
435 
436 	mutex_lock(&pyra->pyra_lock);
437 	memcpy(buf, ((char const *)&pyra->settings) + off, count);
438 	mutex_unlock(&pyra->pyra_lock);
439 
440 	return count;
441 }
442 
443 static ssize_t pyra_sysfs_write_settings(struct file *fp,
444 		struct kobject *kobj, struct bin_attribute *attr, char *buf,
445 		loff_t off, size_t count)
446 {
447 	struct device *dev =
448 			container_of(kobj, struct device, kobj)->parent->parent;
449 	struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
450 	struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
451 	int retval = 0;
452 	int difference;
453 
454 	if (off != 0 || count != sizeof(struct pyra_settings))
455 		return -EINVAL;
456 
457 	mutex_lock(&pyra->pyra_lock);
458 	difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings));
459 	if (difference) {
460 		retval = pyra_set_settings(usb_dev,
461 				(struct pyra_settings const *)buf);
462 		if (!retval)
463 			memcpy(&pyra->settings, buf,
464 					sizeof(struct pyra_settings));
465 	}
466 	mutex_unlock(&pyra->pyra_lock);
467 
468 	if (retval)
469 		return retval;
470 
471 	profile_activated(pyra, pyra->settings.startup_profile);
472 
473 	return sizeof(struct pyra_settings);
474 }
475 
476 
477 static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
478 		struct device_attribute *attr, char *buf)
479 {
480 	struct pyra_device *pyra =
481 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
482 	return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
483 }
484 
485 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
486 		struct device_attribute *attr, char *buf)
487 {
488 	struct pyra_device *pyra =
489 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
490 	return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile);
491 }
492 
493 static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
494 		struct device_attribute *attr, char *buf)
495 {
496 	struct pyra_device *pyra =
497 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
498 	return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version);
499 }
500 
501 static ssize_t pyra_sysfs_show_startup_profile(struct device *dev,
502 		struct device_attribute *attr, char *buf)
503 {
504 	struct pyra_device *pyra =
505 			hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
506 	return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile);
507 }
508 
509 static struct device_attribute pyra_attributes[] = {
510 	__ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
511 	__ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
512 	__ATTR(firmware_version, 0440,
513 			pyra_sysfs_show_firmware_version, NULL),
514 	__ATTR(startup_profile, 0440,
515 			pyra_sysfs_show_startup_profile, NULL),
516 	__ATTR_NULL
517 };
518 
519 static struct bin_attribute pyra_bin_attributes[] = {
520 	{
521 		.attr = { .name = "profile_settings", .mode = 0220 },
522 		.size = sizeof(struct pyra_profile_settings),
523 		.write = pyra_sysfs_write_profile_settings
524 	},
525 	{
526 		.attr = { .name = "profile1_settings", .mode = 0440 },
527 		.size = sizeof(struct pyra_profile_settings),
528 		.read = pyra_sysfs_read_profile1_settings
529 	},
530 	{
531 		.attr = { .name = "profile2_settings", .mode = 0440 },
532 		.size = sizeof(struct pyra_profile_settings),
533 		.read = pyra_sysfs_read_profile2_settings
534 	},
535 	{
536 		.attr = { .name = "profile3_settings", .mode = 0440 },
537 		.size = sizeof(struct pyra_profile_settings),
538 		.read = pyra_sysfs_read_profile3_settings
539 	},
540 	{
541 		.attr = { .name = "profile4_settings", .mode = 0440 },
542 		.size = sizeof(struct pyra_profile_settings),
543 		.read = pyra_sysfs_read_profile4_settings
544 	},
545 	{
546 		.attr = { .name = "profile5_settings", .mode = 0440 },
547 		.size = sizeof(struct pyra_profile_settings),
548 		.read = pyra_sysfs_read_profile5_settings
549 	},
550 	{
551 		.attr = { .name = "profile_buttons", .mode = 0220 },
552 		.size = sizeof(struct pyra_profile_buttons),
553 		.write = pyra_sysfs_write_profile_buttons
554 	},
555 	{
556 		.attr = { .name = "profile1_buttons", .mode = 0440 },
557 		.size = sizeof(struct pyra_profile_buttons),
558 		.read = pyra_sysfs_read_profile1_buttons
559 	},
560 	{
561 		.attr = { .name = "profile2_buttons", .mode = 0440 },
562 		.size = sizeof(struct pyra_profile_buttons),
563 		.read = pyra_sysfs_read_profile2_buttons
564 	},
565 	{
566 		.attr = { .name = "profile3_buttons", .mode = 0440 },
567 		.size = sizeof(struct pyra_profile_buttons),
568 		.read = pyra_sysfs_read_profile3_buttons
569 	},
570 	{
571 		.attr = { .name = "profile4_buttons", .mode = 0440 },
572 		.size = sizeof(struct pyra_profile_buttons),
573 		.read = pyra_sysfs_read_profile4_buttons
574 	},
575 	{
576 		.attr = { .name = "profile5_buttons", .mode = 0440 },
577 		.size = sizeof(struct pyra_profile_buttons),
578 		.read = pyra_sysfs_read_profile5_buttons
579 	},
580 	{
581 		.attr = { .name = "settings", .mode = 0660 },
582 		.size = sizeof(struct pyra_settings),
583 		.read = pyra_sysfs_read_settings,
584 		.write = pyra_sysfs_write_settings
585 	},
586 	__ATTR_NULL
587 };
588 
589 static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
590 		struct pyra_device *pyra)
591 {
592 	struct pyra_info *info;
593 	int retval, i;
594 
595 	mutex_init(&pyra->pyra_lock);
596 
597 	info = kmalloc(sizeof(struct pyra_info), GFP_KERNEL);
598 	if (!info)
599 		return -ENOMEM;
600 	retval = pyra_get_info(usb_dev, info);
601 	if (retval) {
602 		kfree(info);
603 		return retval;
604 	}
605 	pyra->firmware_version = info->firmware_version;
606 	kfree(info);
607 
608 	retval = pyra_get_settings(usb_dev, &pyra->settings);
609 	if (retval)
610 		return retval;
611 
612 	for (i = 0; i < 5; ++i) {
613 		retval = pyra_get_profile_settings(usb_dev,
614 				&pyra->profile_settings[i], i);
615 		if (retval)
616 			return retval;
617 
618 		retval = pyra_get_profile_buttons(usb_dev,
619 				&pyra->profile_buttons[i], i);
620 		if (retval)
621 			return retval;
622 	}
623 
624 	profile_activated(pyra, pyra->settings.startup_profile);
625 
626 	return 0;
627 }
628 
629 static int pyra_init_specials(struct hid_device *hdev)
630 {
631 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
632 	struct usb_device *usb_dev = interface_to_usbdev(intf);
633 	struct pyra_device *pyra;
634 	int retval;
635 
636 	if (intf->cur_altsetting->desc.bInterfaceProtocol
637 			== USB_INTERFACE_PROTOCOL_MOUSE) {
638 
639 		pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
640 		if (!pyra) {
641 			hid_err(hdev, "can't alloc device descriptor\n");
642 			return -ENOMEM;
643 		}
644 		hid_set_drvdata(hdev, pyra);
645 
646 		retval = pyra_init_pyra_device_struct(usb_dev, pyra);
647 		if (retval) {
648 			hid_err(hdev, "couldn't init struct pyra_device\n");
649 			goto exit_free;
650 		}
651 
652 		retval = roccat_connect(pyra_class, hdev);
653 		if (retval < 0) {
654 			hid_err(hdev, "couldn't init char dev\n");
655 		} else {
656 			pyra->chrdev_minor = retval;
657 			pyra->roccat_claimed = 1;
658 		}
659 	} else {
660 		hid_set_drvdata(hdev, NULL);
661 	}
662 
663 	return 0;
664 exit_free:
665 	kfree(pyra);
666 	return retval;
667 }
668 
669 static void pyra_remove_specials(struct hid_device *hdev)
670 {
671 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
672 	struct pyra_device *pyra;
673 
674 	if (intf->cur_altsetting->desc.bInterfaceProtocol
675 			== USB_INTERFACE_PROTOCOL_MOUSE) {
676 		pyra = hid_get_drvdata(hdev);
677 		if (pyra->roccat_claimed)
678 			roccat_disconnect(pyra->chrdev_minor);
679 		kfree(hid_get_drvdata(hdev));
680 	}
681 }
682 
683 static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
684 {
685 	int retval;
686 
687 	retval = hid_parse(hdev);
688 	if (retval) {
689 		hid_err(hdev, "parse failed\n");
690 		goto exit;
691 	}
692 
693 	retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
694 	if (retval) {
695 		hid_err(hdev, "hw start failed\n");
696 		goto exit;
697 	}
698 
699 	retval = pyra_init_specials(hdev);
700 	if (retval) {
701 		hid_err(hdev, "couldn't install mouse\n");
702 		goto exit_stop;
703 	}
704 	return 0;
705 
706 exit_stop:
707 	hid_hw_stop(hdev);
708 exit:
709 	return retval;
710 }
711 
712 static void pyra_remove(struct hid_device *hdev)
713 {
714 	pyra_remove_specials(hdev);
715 	hid_hw_stop(hdev);
716 }
717 
718 static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
719 		u8 const *data)
720 {
721 	struct pyra_mouse_event_button const *button_event;
722 
723 	switch (data[0]) {
724 	case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
725 		button_event = (struct pyra_mouse_event_button const *)data;
726 		switch (button_event->type) {
727 		case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
728 			profile_activated(pyra, button_event->data1 - 1);
729 			break;
730 		case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
731 			pyra->actual_cpi = button_event->data1;
732 			break;
733 		}
734 		break;
735 	}
736 }
737 
738 static void pyra_report_to_chrdev(struct pyra_device const *pyra,
739 		u8 const *data)
740 {
741 	struct pyra_roccat_report roccat_report;
742 	struct pyra_mouse_event_button const *button_event;
743 
744 	if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
745 		return;
746 
747 	button_event = (struct pyra_mouse_event_button const *)data;
748 
749 	switch (button_event->type) {
750 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
751 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
752 		roccat_report.type = button_event->type;
753 		roccat_report.value = button_event->data1;
754 		roccat_report.key = 0;
755 		roccat_report_event(pyra->chrdev_minor,
756 				(uint8_t const *)&roccat_report,
757 				sizeof(struct pyra_roccat_report));
758 		break;
759 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
760 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
761 	case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
762 		if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
763 			roccat_report.type = button_event->type;
764 			roccat_report.key = button_event->data1;
765 			/*
766 			 * pyra reports profile numbers with range 1-5.
767 			 * Keeping this behaviour.
768 			 */
769 			roccat_report.value = pyra->actual_profile + 1;
770 			roccat_report_event(pyra->chrdev_minor,
771 					(uint8_t const *)&roccat_report,
772 					sizeof(struct pyra_roccat_report));
773 		}
774 		break;
775 	}
776 }
777 
778 static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
779 		u8 *data, int size)
780 {
781 	struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
782 	struct pyra_device *pyra = hid_get_drvdata(hdev);
783 
784 	if (intf->cur_altsetting->desc.bInterfaceProtocol
785 			!= USB_INTERFACE_PROTOCOL_MOUSE)
786 		return 0;
787 
788 	pyra_keep_values_up_to_date(pyra, data);
789 
790 	if (pyra->roccat_claimed)
791 		pyra_report_to_chrdev(pyra, data);
792 
793 	return 0;
794 }
795 
796 static const struct hid_device_id pyra_devices[] = {
797 	{ HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
798 			USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
799 	/* TODO add USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS after testing */
800 	{ }
801 };
802 
803 MODULE_DEVICE_TABLE(hid, pyra_devices);
804 
805 static struct hid_driver pyra_driver = {
806 		.name = "pyra",
807 		.id_table = pyra_devices,
808 		.probe = pyra_probe,
809 		.remove = pyra_remove,
810 		.raw_event = pyra_raw_event
811 };
812 
813 static int __init pyra_init(void)
814 {
815 	int retval;
816 
817 	/* class name has to be same as driver name */
818 	pyra_class = class_create(THIS_MODULE, "pyra");
819 	if (IS_ERR(pyra_class))
820 		return PTR_ERR(pyra_class);
821 	pyra_class->dev_attrs = pyra_attributes;
822 	pyra_class->dev_bin_attrs = pyra_bin_attributes;
823 
824 	retval = hid_register_driver(&pyra_driver);
825 	if (retval)
826 		class_destroy(pyra_class);
827 	return retval;
828 }
829 
830 static void __exit pyra_exit(void)
831 {
832 	class_destroy(pyra_class);
833 	hid_unregister_driver(&pyra_driver);
834 }
835 
836 module_init(pyra_init);
837 module_exit(pyra_exit);
838 
839 MODULE_AUTHOR("Stefan Achatz");
840 MODULE_DESCRIPTION("USB Roccat Pyra driver");
841 MODULE_LICENSE("GPL v2");
842