treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / hid / hid-roccat-pyra.c
blob989927defe8db84d6d1916424a9c1d50fc5c3911
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Roccat Pyra driver for Linux
5 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
6 */
8 /*
9 */
12 * Roccat Pyra is a mobile gamer mouse which comes in wired and wireless
13 * variant. Wireless variant is not tested.
14 * Userland tools can be found at http://sourceforge.net/projects/roccat
17 #include <linux/device.h>
18 #include <linux/input.h>
19 #include <linux/hid.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/hid-roccat.h>
23 #include "hid-ids.h"
24 #include "hid-roccat-common.h"
25 #include "hid-roccat-pyra.h"
27 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
29 /* pyra_class is used for creating sysfs attributes via roccat char device */
30 static struct class *pyra_class;
32 static void profile_activated(struct pyra_device *pyra,
33 unsigned int new_profile)
35 if (new_profile >= ARRAY_SIZE(pyra->profile_settings))
36 return;
37 pyra->actual_profile = new_profile;
38 pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
41 static int pyra_send_control(struct usb_device *usb_dev, int value,
42 enum pyra_control_requests request)
44 struct roccat_common2_control control;
46 if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
47 request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
48 (value < 0 || value > 4))
49 return -EINVAL;
51 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
52 control.value = value;
53 control.request = request;
55 return roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL,
56 &control, sizeof(struct roccat_common2_control));
59 static int pyra_get_profile_settings(struct usb_device *usb_dev,
60 struct pyra_profile_settings *buf, int number)
62 int retval;
63 retval = pyra_send_control(usb_dev, number,
64 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
65 if (retval)
66 return retval;
67 return roccat_common2_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS,
68 buf, PYRA_SIZE_PROFILE_SETTINGS);
71 static int pyra_get_settings(struct usb_device *usb_dev,
72 struct pyra_settings *buf)
74 return roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
75 buf, PYRA_SIZE_SETTINGS);
78 static int pyra_set_settings(struct usb_device *usb_dev,
79 struct pyra_settings const *settings)
81 return roccat_common2_send_with_status(usb_dev,
82 PYRA_COMMAND_SETTINGS, settings,
83 PYRA_SIZE_SETTINGS);
86 static ssize_t pyra_sysfs_read(struct file *fp, struct kobject *kobj,
87 char *buf, loff_t off, size_t count,
88 size_t real_size, uint command)
90 struct device *dev = kobj_to_dev(kobj)->parent->parent;
91 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
92 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
93 int retval;
95 if (off >= real_size)
96 return 0;
98 if (off != 0 || count != real_size)
99 return -EINVAL;
101 mutex_lock(&pyra->pyra_lock);
102 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
103 mutex_unlock(&pyra->pyra_lock);
105 if (retval)
106 return retval;
108 return real_size;
111 static ssize_t pyra_sysfs_write(struct file *fp, struct kobject *kobj,
112 void const *buf, loff_t off, size_t count,
113 size_t real_size, uint command)
115 struct device *dev = kobj_to_dev(kobj)->parent->parent;
116 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
117 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
118 int retval;
120 if (off != 0 || count != real_size)
121 return -EINVAL;
123 mutex_lock(&pyra->pyra_lock);
124 retval = roccat_common2_send_with_status(usb_dev, command, (void *)buf, real_size);
125 mutex_unlock(&pyra->pyra_lock);
127 if (retval)
128 return retval;
130 return real_size;
133 #define PYRA_SYSFS_W(thingy, THINGY) \
134 static ssize_t pyra_sysfs_write_ ## thingy(struct file *fp, \
135 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
136 loff_t off, size_t count) \
138 return pyra_sysfs_write(fp, kobj, buf, off, count, \
139 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
142 #define PYRA_SYSFS_R(thingy, THINGY) \
143 static ssize_t pyra_sysfs_read_ ## thingy(struct file *fp, \
144 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
145 loff_t off, size_t count) \
147 return pyra_sysfs_read(fp, kobj, buf, off, count, \
148 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
151 #define PYRA_SYSFS_RW(thingy, THINGY) \
152 PYRA_SYSFS_W(thingy, THINGY) \
153 PYRA_SYSFS_R(thingy, THINGY)
155 #define PYRA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
156 PYRA_SYSFS_RW(thingy, THINGY); \
157 static struct bin_attribute bin_attr_##thingy = { \
158 .attr = { .name = #thingy, .mode = 0660 }, \
159 .size = PYRA_SIZE_ ## THINGY, \
160 .read = pyra_sysfs_read_ ## thingy, \
161 .write = pyra_sysfs_write_ ## thingy \
164 #define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \
165 PYRA_SYSFS_R(thingy, THINGY); \
166 static struct bin_attribute bin_attr_##thingy = { \
167 .attr = { .name = #thingy, .mode = 0440 }, \
168 .size = PYRA_SIZE_ ## THINGY, \
169 .read = pyra_sysfs_read_ ## thingy, \
172 #define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \
173 PYRA_SYSFS_W(thingy, THINGY); \
174 static struct bin_attribute bin_attr_##thingy = { \
175 .attr = { .name = #thingy, .mode = 0220 }, \
176 .size = PYRA_SIZE_ ## THINGY, \
177 .write = pyra_sysfs_write_ ## thingy \
180 PYRA_BIN_ATTRIBUTE_W(control, CONTROL);
181 PYRA_BIN_ATTRIBUTE_RW(info, INFO);
182 PYRA_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
183 PYRA_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
185 static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
186 struct kobject *kobj, struct bin_attribute *attr, char *buf,
187 loff_t off, size_t count)
189 struct device *dev = kobj_to_dev(kobj)->parent->parent;
190 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
191 ssize_t retval;
193 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
194 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
195 if (retval)
196 return retval;
198 return pyra_sysfs_read(fp, kobj, buf, off, count,
199 PYRA_SIZE_PROFILE_SETTINGS,
200 PYRA_COMMAND_PROFILE_SETTINGS);
203 static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
204 struct kobject *kobj, struct bin_attribute *attr, char *buf,
205 loff_t off, size_t count)
207 struct device *dev = kobj_to_dev(kobj)->parent->parent;
208 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
209 ssize_t retval;
211 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
212 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
213 if (retval)
214 return retval;
216 return pyra_sysfs_read(fp, kobj, buf, off, count,
217 PYRA_SIZE_PROFILE_BUTTONS,
218 PYRA_COMMAND_PROFILE_BUTTONS);
221 #define PROFILE_ATTR(number) \
222 static struct bin_attribute bin_attr_profile##number##_settings = { \
223 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
224 .size = PYRA_SIZE_PROFILE_SETTINGS, \
225 .read = pyra_sysfs_read_profilex_settings, \
226 .private = &profile_numbers[number-1], \
227 }; \
228 static struct bin_attribute bin_attr_profile##number##_buttons = { \
229 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
230 .size = PYRA_SIZE_PROFILE_BUTTONS, \
231 .read = pyra_sysfs_read_profilex_buttons, \
232 .private = &profile_numbers[number-1], \
234 PROFILE_ATTR(1);
235 PROFILE_ATTR(2);
236 PROFILE_ATTR(3);
237 PROFILE_ATTR(4);
238 PROFILE_ATTR(5);
240 static ssize_t pyra_sysfs_write_settings(struct file *fp,
241 struct kobject *kobj, struct bin_attribute *attr, char *buf,
242 loff_t off, size_t count)
244 struct device *dev = kobj_to_dev(kobj)->parent->parent;
245 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
246 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
247 int retval = 0;
248 struct pyra_roccat_report roccat_report;
249 struct pyra_settings const *settings;
251 if (off != 0 || count != PYRA_SIZE_SETTINGS)
252 return -EINVAL;
254 settings = (struct pyra_settings const *)buf;
255 if (settings->startup_profile >= ARRAY_SIZE(pyra->profile_settings))
256 return -EINVAL;
258 mutex_lock(&pyra->pyra_lock);
260 retval = pyra_set_settings(usb_dev, settings);
261 if (retval) {
262 mutex_unlock(&pyra->pyra_lock);
263 return retval;
266 profile_activated(pyra, settings->startup_profile);
268 roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
269 roccat_report.value = settings->startup_profile + 1;
270 roccat_report.key = 0;
271 roccat_report_event(pyra->chrdev_minor,
272 (uint8_t const *)&roccat_report);
274 mutex_unlock(&pyra->pyra_lock);
275 return PYRA_SIZE_SETTINGS;
278 PYRA_SYSFS_R(settings, SETTINGS);
279 static struct bin_attribute bin_attr_settings =
280 __BIN_ATTR(settings, (S_IWUSR | S_IRUGO),
281 pyra_sysfs_read_settings, pyra_sysfs_write_settings,
282 PYRA_SIZE_SETTINGS);
284 static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
285 struct device_attribute *attr, char *buf)
287 struct pyra_device *pyra =
288 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
289 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
291 static DEVICE_ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL);
293 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
294 struct device_attribute *attr, char *buf)
296 struct pyra_device *pyra =
297 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
298 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
299 struct pyra_settings settings;
301 mutex_lock(&pyra->pyra_lock);
302 roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
303 &settings, PYRA_SIZE_SETTINGS);
304 mutex_unlock(&pyra->pyra_lock);
306 return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile);
308 static DEVICE_ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
309 static DEVICE_ATTR(startup_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
311 static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
312 struct device_attribute *attr, char *buf)
314 struct pyra_device *pyra;
315 struct usb_device *usb_dev;
316 struct pyra_info info;
318 dev = dev->parent->parent;
319 pyra = hid_get_drvdata(dev_get_drvdata(dev));
320 usb_dev = interface_to_usbdev(to_usb_interface(dev));
322 mutex_lock(&pyra->pyra_lock);
323 roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
324 &info, PYRA_SIZE_INFO);
325 mutex_unlock(&pyra->pyra_lock);
327 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
329 static DEVICE_ATTR(firmware_version, 0440, pyra_sysfs_show_firmware_version,
330 NULL);
332 static struct attribute *pyra_attrs[] = {
333 &dev_attr_actual_cpi.attr,
334 &dev_attr_actual_profile.attr,
335 &dev_attr_firmware_version.attr,
336 &dev_attr_startup_profile.attr,
337 NULL,
340 static struct bin_attribute *pyra_bin_attributes[] = {
341 &bin_attr_control,
342 &bin_attr_info,
343 &bin_attr_profile_settings,
344 &bin_attr_profile_buttons,
345 &bin_attr_settings,
346 &bin_attr_profile1_settings,
347 &bin_attr_profile2_settings,
348 &bin_attr_profile3_settings,
349 &bin_attr_profile4_settings,
350 &bin_attr_profile5_settings,
351 &bin_attr_profile1_buttons,
352 &bin_attr_profile2_buttons,
353 &bin_attr_profile3_buttons,
354 &bin_attr_profile4_buttons,
355 &bin_attr_profile5_buttons,
356 NULL,
359 static const struct attribute_group pyra_group = {
360 .attrs = pyra_attrs,
361 .bin_attrs = pyra_bin_attributes,
364 static const struct attribute_group *pyra_groups[] = {
365 &pyra_group,
366 NULL,
369 static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
370 struct pyra_device *pyra)
372 struct pyra_settings settings;
373 int retval, i;
375 mutex_init(&pyra->pyra_lock);
377 retval = pyra_get_settings(usb_dev, &settings);
378 if (retval)
379 return retval;
381 for (i = 0; i < 5; ++i) {
382 retval = pyra_get_profile_settings(usb_dev,
383 &pyra->profile_settings[i], i);
384 if (retval)
385 return retval;
388 profile_activated(pyra, settings.startup_profile);
390 return 0;
393 static int pyra_init_specials(struct hid_device *hdev)
395 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
396 struct usb_device *usb_dev = interface_to_usbdev(intf);
397 struct pyra_device *pyra;
398 int retval;
400 if (intf->cur_altsetting->desc.bInterfaceProtocol
401 == USB_INTERFACE_PROTOCOL_MOUSE) {
403 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
404 if (!pyra) {
405 hid_err(hdev, "can't alloc device descriptor\n");
406 return -ENOMEM;
408 hid_set_drvdata(hdev, pyra);
410 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
411 if (retval) {
412 hid_err(hdev, "couldn't init struct pyra_device\n");
413 goto exit_free;
416 retval = roccat_connect(pyra_class, hdev,
417 sizeof(struct pyra_roccat_report));
418 if (retval < 0) {
419 hid_err(hdev, "couldn't init char dev\n");
420 } else {
421 pyra->chrdev_minor = retval;
422 pyra->roccat_claimed = 1;
424 } else {
425 hid_set_drvdata(hdev, NULL);
428 return 0;
429 exit_free:
430 kfree(pyra);
431 return retval;
434 static void pyra_remove_specials(struct hid_device *hdev)
436 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
437 struct pyra_device *pyra;
439 if (intf->cur_altsetting->desc.bInterfaceProtocol
440 == USB_INTERFACE_PROTOCOL_MOUSE) {
441 pyra = hid_get_drvdata(hdev);
442 if (pyra->roccat_claimed)
443 roccat_disconnect(pyra->chrdev_minor);
444 kfree(hid_get_drvdata(hdev));
448 static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
450 int retval;
452 retval = hid_parse(hdev);
453 if (retval) {
454 hid_err(hdev, "parse failed\n");
455 goto exit;
458 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
459 if (retval) {
460 hid_err(hdev, "hw start failed\n");
461 goto exit;
464 retval = pyra_init_specials(hdev);
465 if (retval) {
466 hid_err(hdev, "couldn't install mouse\n");
467 goto exit_stop;
469 return 0;
471 exit_stop:
472 hid_hw_stop(hdev);
473 exit:
474 return retval;
477 static void pyra_remove(struct hid_device *hdev)
479 pyra_remove_specials(hdev);
480 hid_hw_stop(hdev);
483 static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
484 u8 const *data)
486 struct pyra_mouse_event_button const *button_event;
488 switch (data[0]) {
489 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
490 button_event = (struct pyra_mouse_event_button const *)data;
491 switch (button_event->type) {
492 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
493 profile_activated(pyra, button_event->data1 - 1);
494 break;
495 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
496 pyra->actual_cpi = button_event->data1;
497 break;
499 break;
503 static void pyra_report_to_chrdev(struct pyra_device const *pyra,
504 u8 const *data)
506 struct pyra_roccat_report roccat_report;
507 struct pyra_mouse_event_button const *button_event;
509 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
510 return;
512 button_event = (struct pyra_mouse_event_button const *)data;
514 switch (button_event->type) {
515 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
516 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
517 roccat_report.type = button_event->type;
518 roccat_report.value = button_event->data1;
519 roccat_report.key = 0;
520 roccat_report_event(pyra->chrdev_minor,
521 (uint8_t const *)&roccat_report);
522 break;
523 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
524 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
525 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
526 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
527 roccat_report.type = button_event->type;
528 roccat_report.key = button_event->data1;
530 * pyra reports profile numbers with range 1-5.
531 * Keeping this behaviour.
533 roccat_report.value = pyra->actual_profile + 1;
534 roccat_report_event(pyra->chrdev_minor,
535 (uint8_t const *)&roccat_report);
537 break;
541 static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
542 u8 *data, int size)
544 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
545 struct pyra_device *pyra = hid_get_drvdata(hdev);
547 if (intf->cur_altsetting->desc.bInterfaceProtocol
548 != USB_INTERFACE_PROTOCOL_MOUSE)
549 return 0;
551 if (pyra == NULL)
552 return 0;
554 pyra_keep_values_up_to_date(pyra, data);
556 if (pyra->roccat_claimed)
557 pyra_report_to_chrdev(pyra, data);
559 return 0;
562 static const struct hid_device_id pyra_devices[] = {
563 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
564 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
565 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
566 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
570 MODULE_DEVICE_TABLE(hid, pyra_devices);
572 static struct hid_driver pyra_driver = {
573 .name = "pyra",
574 .id_table = pyra_devices,
575 .probe = pyra_probe,
576 .remove = pyra_remove,
577 .raw_event = pyra_raw_event
580 static int __init pyra_init(void)
582 int retval;
584 /* class name has to be same as driver name */
585 pyra_class = class_create(THIS_MODULE, "pyra");
586 if (IS_ERR(pyra_class))
587 return PTR_ERR(pyra_class);
588 pyra_class->dev_groups = pyra_groups;
590 retval = hid_register_driver(&pyra_driver);
591 if (retval)
592 class_destroy(pyra_class);
593 return retval;
596 static void __exit pyra_exit(void)
598 hid_unregister_driver(&pyra_driver);
599 class_destroy(pyra_class);
602 module_init(pyra_init);
603 module_exit(pyra_exit);
605 MODULE_AUTHOR("Stefan Achatz");
606 MODULE_DESCRIPTION("USB Roccat Pyra driver");
607 MODULE_LICENSE("GPL v2");