HID: Add and use hid_<level>: dev_<level> equivalents
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / hid / hid-roccat-pyra.c
CommitLineData
cb7cf3da
SA
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
30static void profile_activated(struct pyra_device *pyra,
31 unsigned int new_profile)
32{
33 pyra->actual_profile = new_profile;
34 pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
35}
36
37static int pyra_send_control(struct usb_device *usb_dev, int value,
38 enum pyra_control_requests request)
39{
40 int len;
41 struct pyra_control control;
42
43 if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
44 request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
45 (value < 0 || value > 4))
46 return -EINVAL;
47
48 control.command = PYRA_COMMAND_CONTROL;
49 control.value = value;
50 control.request = request;
51
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);
58
59 if (len != sizeof(struct pyra_control))
60 return len;
61
62 return 0;
63}
64
65static int pyra_receive_control_status(struct usb_device *usb_dev)
66{
67 int len;
68 struct pyra_control control;
69
70 do {
71 msleep(10);
72
73 len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
74 USB_REQ_CLEAR_FEATURE,
75 USB_TYPE_CLASS | USB_RECIP_INTERFACE |
76 USB_DIR_IN,
77 PYRA_USB_COMMAND_CONTROL, 0, (char *)&control,
78 sizeof(struct pyra_control),
79 USB_CTRL_SET_TIMEOUT);
80
81 /* requested too early, try again */
82 } while (len == -EPROTO);
83
84 if (len == sizeof(struct pyra_control) &&
85 control.command == PYRA_COMMAND_CONTROL &&
86 control.request == PYRA_CONTROL_REQUEST_STATUS &&
87 control.value == 1)
88 return 0;
89 else {
4291ee30
JP
90 hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n",
91 control.request, control.value);
cb7cf3da
SA
92 return -EINVAL;
93 }
94}
95
96static int pyra_get_profile_settings(struct usb_device *usb_dev,
97 struct pyra_profile_settings *buf, int number)
98{
99 int retval;
100
101 retval = pyra_send_control(usb_dev, number,
102 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
103
104 if (retval)
105 return retval;
106
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);
113
114 if (retval != sizeof(struct pyra_profile_settings))
115 return retval;
116
117 return 0;
118}
119
120static int pyra_get_profile_buttons(struct usb_device *usb_dev,
121 struct pyra_profile_buttons *buf, int number)
122{
123 int retval;
124
125 retval = pyra_send_control(usb_dev, number,
126 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
127
128 if (retval)
129 return retval;
130
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);
137
138 if (retval != sizeof(struct pyra_profile_buttons))
139 return retval;
140
141 return 0;
142}
143
144static int pyra_get_settings(struct usb_device *usb_dev,
145 struct pyra_settings *buf)
146{
147 int len;
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))
154 return -EIO;
155 return 0;
156}
157
158static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf)
159{
160 int len;
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))
167 return -EIO;
168 return 0;
169}
170
171static int pyra_set_profile_settings(struct usb_device *usb_dev,
172 struct pyra_profile_settings const *settings)
173{
174 int len;
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))
182 return -EIO;
183 if (pyra_receive_control_status(usb_dev))
184 return -EIO;
185 return 0;
186}
187
188static int pyra_set_profile_buttons(struct usb_device *usb_dev,
189 struct pyra_profile_buttons const *buttons)
190{
191 int len;
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))
199 return -EIO;
200 if (pyra_receive_control_status(usb_dev))
201 return -EIO;
202 return 0;
203}
204
205static int pyra_set_settings(struct usb_device *usb_dev,
206 struct pyra_settings const *settings)
207{
208 int len;
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))
215 return -EIO;
216 if (pyra_receive_control_status(usb_dev))
217 return -EIO;
218 return 0;
219}
220
221static 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)
224{
225 struct device *dev = container_of(kobj, struct device, kobj);
226 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
227
228 if (off >= sizeof(struct pyra_profile_settings))
229 return 0;
230
231 if (off + count > sizeof(struct pyra_profile_settings))
232 count = sizeof(struct pyra_profile_settings) - off;
233
234 mutex_lock(&pyra->pyra_lock);
235 memcpy(buf, ((char const *)&pyra->profile_settings[number]) + off,
236 count);
237 mutex_unlock(&pyra->pyra_lock);
238
239 return count;
240}
241
242static 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)
245{
246 return pyra_sysfs_read_profilex_settings(fp, kobj,
247 attr, buf, off, count, 0);
248}
249
250static 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)
253{
254 return pyra_sysfs_read_profilex_settings(fp, kobj,
255 attr, buf, off, count, 1);
256}
257
258static 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)
261{
262 return pyra_sysfs_read_profilex_settings(fp, kobj,
263 attr, buf, off, count, 2);
264}
265
266static 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)
269{
270 return pyra_sysfs_read_profilex_settings(fp, kobj,
271 attr, buf, off, count, 3);
272}
273
274static 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)
277{
278 return pyra_sysfs_read_profilex_settings(fp, kobj,
279 attr, buf, off, count, 4);
280}
281
282static 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)
285{
286 struct device *dev = container_of(kobj, struct device, kobj);
287 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
288
289 if (off >= sizeof(struct pyra_profile_buttons))
290 return 0;
291
292 if (off + count > sizeof(struct pyra_profile_buttons))
293 count = sizeof(struct pyra_profile_buttons) - off;
294
295 mutex_lock(&pyra->pyra_lock);
296 memcpy(buf, ((char const *)&pyra->profile_buttons[number]) + off,
297 count);
298 mutex_unlock(&pyra->pyra_lock);
299
300 return count;
301}
302
303static 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)
306{
307 return pyra_sysfs_read_profilex_buttons(fp, kobj,
308 attr, buf, off, count, 0);
309}
310
311static 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)
314{
315 return pyra_sysfs_read_profilex_buttons(fp, kobj,
316 attr, buf, off, count, 1);
317}
318
319static 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)
322{
323 return pyra_sysfs_read_profilex_buttons(fp, kobj,
324 attr, buf, off, count, 2);
325}
326
327static 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)
330{
331 return pyra_sysfs_read_profilex_buttons(fp, kobj,
332 attr, buf, off, count, 3);
333}
334
335static 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)
338{
339 return pyra_sysfs_read_profilex_buttons(fp, kobj,
340 attr, buf, off, count, 4);
341}
342
343static 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)
346{
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));
350 int retval = 0;
351 int difference;
352 int profile_number;
353 struct pyra_profile_settings *profile_settings;
354
355 if (off != 0 || count != sizeof(struct pyra_profile_settings))
356 return -EINVAL;
357
358 profile_number = ((struct pyra_profile_settings const *)buf)->number;
359 profile_settings = &pyra->profile_settings[profile_number];
360
361 mutex_lock(&pyra->pyra_lock);
362 difference = memcmp(buf, profile_settings,
363 sizeof(struct pyra_profile_settings));
364 if (difference) {
365 retval = pyra_set_profile_settings(usb_dev,
366 (struct pyra_profile_settings const *)buf);
367 if (!retval)
368 memcpy(profile_settings, buf,
369 sizeof(struct pyra_profile_settings));
370 }
371 mutex_unlock(&pyra->pyra_lock);
372
373 if (retval)
374 return retval;
375
376 return sizeof(struct pyra_profile_settings);
377}
378
379static 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)
382{
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));
386 int retval = 0;
387 int difference;
388 int profile_number;
389 struct pyra_profile_buttons *profile_buttons;
390
391 if (off != 0 || count != sizeof(struct pyra_profile_buttons))
392 return -EINVAL;
393
394 profile_number = ((struct pyra_profile_buttons const *)buf)->number;
395 profile_buttons = &pyra->profile_buttons[profile_number];
396
397 mutex_lock(&pyra->pyra_lock);
398 difference = memcmp(buf, profile_buttons,
399 sizeof(struct pyra_profile_buttons));
400 if (difference) {
401 retval = pyra_set_profile_buttons(usb_dev,
402 (struct pyra_profile_buttons const *)buf);
403 if (!retval)
404 memcpy(profile_buttons, buf,
405 sizeof(struct pyra_profile_buttons));
406 }
407 mutex_unlock(&pyra->pyra_lock);
408
409 if (retval)
410 return retval;
411
412 return sizeof(struct pyra_profile_buttons);
413}
414
415static 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)
418{
419 struct device *dev = container_of(kobj, struct device, kobj);
420 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
421
422 if (off >= sizeof(struct pyra_settings))
423 return 0;
424
425 if (off + count > sizeof(struct pyra_settings))
426 count = sizeof(struct pyra_settings) - off;
427
428 mutex_lock(&pyra->pyra_lock);
429 memcpy(buf, ((char const *)&pyra->settings) + off, count);
430 mutex_unlock(&pyra->pyra_lock);
431
432 return count;
433}
434
435static 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)
438{
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));
442 int retval = 0;
443 int difference;
444
445 if (off != 0 || count != sizeof(struct pyra_settings))
446 return -EINVAL;
447
448 mutex_lock(&pyra->pyra_lock);
449 difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings));
450 if (difference) {
451 retval = pyra_set_settings(usb_dev,
452 (struct pyra_settings const *)buf);
453 if (!retval)
454 memcpy(&pyra->settings, buf,
455 sizeof(struct pyra_settings));
456 }
457 mutex_unlock(&pyra->pyra_lock);
458
459 if (retval)
460 return retval;
461
462 profile_activated(pyra, pyra->settings.startup_profile);
463
464 return sizeof(struct pyra_settings);
465}
466
467
468static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
469 struct device_attribute *attr, char *buf)
470{
471 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
472 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
473}
474
475static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
476 struct device_attribute *attr, char *buf)
477{
478 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
479 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile);
480}
481
482static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
483 struct device_attribute *attr, char *buf)
484{
485 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
486 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version);
487}
488
489static ssize_t pyra_sysfs_show_startup_profile(struct device *dev,
490 struct device_attribute *attr, char *buf)
491{
492 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
493 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile);
494}
495
496static DEVICE_ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL);
497
498static DEVICE_ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
499
500static DEVICE_ATTR(firmware_version, 0440,
501 pyra_sysfs_show_firmware_version, NULL);
502
503static DEVICE_ATTR(startup_profile, 0440,
504 pyra_sysfs_show_startup_profile, NULL);
505
506static 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,
511 NULL
512};
513
514static struct attribute_group pyra_attribute_group = {
515 .attrs = pyra_attributes
516};
517
518static 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
522};
523
524static 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
528};
529
530static 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
534};
535
536static 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
540};
541
542static 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
546};
547
548static 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
552};
553
554static 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
558};
559
560static 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
564};
565
566static 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
570};
571
572static 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
576};
577
578static 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
582};
583
584static 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
588};
589
590static 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
595};
596
597static int pyra_create_sysfs_attributes(struct usb_interface *intf)
598{
599 int retval;
600
601 retval = sysfs_create_group(&intf->dev.kobj, &pyra_attribute_group);
602 if (retval)
603 goto exit_1;
604
605 retval = sysfs_create_bin_file(&intf->dev.kobj,
606 &pyra_profile_settings_attr);
607 if (retval)
608 goto exit_2;
609
610 retval = sysfs_create_bin_file(&intf->dev.kobj,
611 &pyra_profile1_settings_attr);
612 if (retval)
613 goto exit_3;
614
615 retval = sysfs_create_bin_file(&intf->dev.kobj,
616 &pyra_profile2_settings_attr);
617 if (retval)
618 goto exit_4;
619
620 retval = sysfs_create_bin_file(&intf->dev.kobj,
621 &pyra_profile3_settings_attr);
622 if (retval)
623 goto exit_5;
624
625 retval = sysfs_create_bin_file(&intf->dev.kobj,
626 &pyra_profile4_settings_attr);
627 if (retval)
628 goto exit_6;
629
630 retval = sysfs_create_bin_file(&intf->dev.kobj,
631 &pyra_profile5_settings_attr);
632 if (retval)
633 goto exit_7;
634
635 retval = sysfs_create_bin_file(&intf->dev.kobj,
636 &pyra_profile_buttons_attr);
637 if (retval)
638 goto exit_8;
639
640 retval = sysfs_create_bin_file(&intf->dev.kobj,
641 &pyra_profile1_buttons_attr);
642 if (retval)
643 goto exit_9;
644
645 retval = sysfs_create_bin_file(&intf->dev.kobj,
646 &pyra_profile2_buttons_attr);
647 if (retval)
648 goto exit_10;
649
650 retval = sysfs_create_bin_file(&intf->dev.kobj,
651 &pyra_profile3_buttons_attr);
652 if (retval)
653 goto exit_11;
654
655 retval = sysfs_create_bin_file(&intf->dev.kobj,
656 &pyra_profile4_buttons_attr);
657 if (retval)
658 goto exit_12;
659
660 retval = sysfs_create_bin_file(&intf->dev.kobj,
661 &pyra_profile5_buttons_attr);
662 if (retval)
663 goto exit_13;
664
665 retval = sysfs_create_bin_file(&intf->dev.kobj,
666 &pyra_settings_attr);
667 if (retval)
668 goto exit_14;
669
670 return 0;
671
672exit_14:
673 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile5_buttons_attr);
674exit_13:
675 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile4_buttons_attr);
676exit_12:
677 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile3_buttons_attr);
678exit_11:
679 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile2_buttons_attr);
680exit_10:
681 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile1_buttons_attr);
682exit_9:
683 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile_buttons_attr);
684exit_8:
685 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile5_settings_attr);
686exit_7:
687 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile4_settings_attr);
688exit_6:
689 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile3_settings_attr);
690exit_5:
691 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile2_settings_attr);
692exit_4:
693 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile1_settings_attr);
694exit_3:
695 sysfs_remove_bin_file(&intf->dev.kobj, &pyra_profile_settings_attr);
696exit_2:
697 sysfs_remove_group(&intf->dev.kobj, &pyra_attribute_group);
698exit_1:
699 return retval;
700}
701
702static void pyra_remove_sysfs_attributes(struct usb_interface *intf)
703{
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);
718}
719
720static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
721 struct pyra_device *pyra)
722{
723 struct pyra_info *info;
724 int retval, i;
725
726 mutex_init(&pyra->pyra_lock);
727
728 info = kmalloc(sizeof(struct pyra_info), GFP_KERNEL);
729 if (!info)
730 return -ENOMEM;
731 retval = pyra_get_info(usb_dev, info);
732 if (retval) {
733 kfree(info);
734 return retval;
735 }
736 pyra->firmware_version = info->firmware_version;
737 kfree(info);
738
739 retval = pyra_get_settings(usb_dev, &pyra->settings);
740 if (retval)
741 return retval;
742
743 for (i = 0; i < 5; ++i) {
744 retval = pyra_get_profile_settings(usb_dev,
745 &pyra->profile_settings[i], i);
746 if (retval)
747 return retval;
748
749 retval = pyra_get_profile_buttons(usb_dev,
750 &pyra->profile_buttons[i], i);
751 if (retval)
752 return retval;
753 }
754
755 profile_activated(pyra, pyra->settings.startup_profile);
756
757 return 0;
758}
759
760static int pyra_init_specials(struct hid_device *hdev)
761{
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;
765 int retval;
766
767 if (intf->cur_altsetting->desc.bInterfaceProtocol
768 == USB_INTERFACE_PROTOCOL_MOUSE) {
769
770 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
771 if (!pyra) {
4291ee30 772 hid_err(hdev, "can't alloc device descriptor\n");
cb7cf3da
SA
773 return -ENOMEM;
774 }
775 hid_set_drvdata(hdev, pyra);
776
777 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
778 if (retval) {
4291ee30 779 hid_err(hdev, "couldn't init struct pyra_device\n");
cb7cf3da
SA
780 goto exit_free;
781 }
782
783 retval = roccat_connect(hdev);
784 if (retval < 0) {
4291ee30 785 hid_err(hdev, "couldn't init char dev\n");
cb7cf3da
SA
786 } else {
787 pyra->chrdev_minor = retval;
788 pyra->roccat_claimed = 1;
789 }
790
791 retval = pyra_create_sysfs_attributes(intf);
792 if (retval) {
4291ee30 793 hid_err(hdev, "cannot create sysfs files\n");
cb7cf3da
SA
794 goto exit_free;
795 }
796 } else {
797 hid_set_drvdata(hdev, NULL);
798 }
799
800 return 0;
801exit_free:
802 kfree(pyra);
803 return retval;
804}
805
806static void pyra_remove_specials(struct hid_device *hdev)
807{
808 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
809 struct pyra_device *pyra;
810
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));
818 }
819}
820
821static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
822{
823 int retval;
824
825 retval = hid_parse(hdev);
826 if (retval) {
4291ee30 827 hid_err(hdev, "parse failed\n");
cb7cf3da
SA
828 goto exit;
829 }
830
831 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
832 if (retval) {
4291ee30 833 hid_err(hdev, "hw start failed\n");
cb7cf3da
SA
834 goto exit;
835 }
836
837 retval = pyra_init_specials(hdev);
838 if (retval) {
4291ee30 839 hid_err(hdev, "couldn't install mouse\n");
cb7cf3da
SA
840 goto exit_stop;
841 }
842 return 0;
843
844exit_stop:
845 hid_hw_stop(hdev);
846exit:
847 return retval;
848}
849
850static void pyra_remove(struct hid_device *hdev)
851{
852 pyra_remove_specials(hdev);
853 hid_hw_stop(hdev);
854}
855
856static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
857 u8 const *data)
858{
859 struct pyra_mouse_event_button const *button_event;
860
861 switch (data[0]) {
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);
867 break;
868 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
869 pyra->actual_cpi = button_event->data1;
870 break;
871 }
872 break;
873 }
874}
875
876static void pyra_report_to_chrdev(struct pyra_device const *pyra,
877 u8 const *data)
878{
879 struct pyra_roccat_report roccat_report;
880 struct pyra_mouse_event_button const *button_event;
881
882 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
883 return;
884
885 button_event = (struct pyra_mouse_event_button const *)data;
886
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));
896 break;
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;
d2b570a5
SA
903 /*
904 * pyra reports profile numbers with range 1-5.
905 * Keeping this behaviour.
906 */
907 roccat_report.value = pyra->actual_profile + 1;
cb7cf3da
SA
908 roccat_report_event(pyra->chrdev_minor,
909 (uint8_t const *)&roccat_report,
910 sizeof(struct pyra_roccat_report));
911 }
912 break;
913 }
914}
915
916static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
917 u8 *data, int size)
918{
919 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
920 struct pyra_device *pyra = hid_get_drvdata(hdev);
921
922 if (intf->cur_altsetting->desc.bInterfaceProtocol
923 != USB_INTERFACE_PROTOCOL_MOUSE)
924 return 0;
925
926 pyra_keep_values_up_to_date(pyra, data);
927
928 if (pyra->roccat_claimed)
929 pyra_report_to_chrdev(pyra, data);
930
931 return 0;
932}
933
934static 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 */
938 { }
939};
940
941MODULE_DEVICE_TABLE(hid, pyra_devices);
942
943static struct hid_driver pyra_driver = {
944 .name = "pyra",
945 .id_table = pyra_devices,
946 .probe = pyra_probe,
947 .remove = pyra_remove,
948 .raw_event = pyra_raw_event
949};
950
951static int __init pyra_init(void)
952{
953 return hid_register_driver(&pyra_driver);
954}
955
956static void __exit pyra_exit(void)
957{
958 hid_unregister_driver(&pyra_driver);
959}
960
961module_init(pyra_init);
962module_exit(pyra_exit);
963
964MODULE_AUTHOR("Stefan Achatz");
965MODULE_DESCRIPTION("USB Roccat Pyra driver");
966MODULE_LICENSE("GPL v2");