e1000e: cleanup PARENTHESIS_ALIGNMENT checkpatch checks
[linux/fpc-iii.git] / drivers / hid / hid-roccat-pyra.c
blobd4f1e3bee5909eed76861acff408b582985462af
1 /*
2 * Roccat Pyra driver for Linux
4 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
5 */
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.
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/module.h>
24 #include <linux/slab.h>
25 #include <linux/hid-roccat.h>
26 #include "hid-ids.h"
27 #include "hid-roccat-common.h"
28 #include "hid-roccat-pyra.h"
30 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
32 /* pyra_class is used for creating sysfs attributes via roccat char device */
33 static struct class *pyra_class;
35 static void profile_activated(struct pyra_device *pyra,
36 unsigned int new_profile)
38 pyra->actual_profile = new_profile;
39 pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
42 static int pyra_send_control(struct usb_device *usb_dev, int value,
43 enum pyra_control_requests request)
45 struct roccat_common2_control control;
47 if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
48 request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
49 (value < 0 || value > 4))
50 return -EINVAL;
52 control.command = ROCCAT_COMMON_COMMAND_CONTROL;
53 control.value = value;
54 control.request = request;
56 return roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL,
57 &control, sizeof(struct roccat_common2_control));
60 static int pyra_get_profile_settings(struct usb_device *usb_dev,
61 struct pyra_profile_settings *buf, int number)
63 int retval;
64 retval = pyra_send_control(usb_dev, number,
65 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
66 if (retval)
67 return retval;
68 return roccat_common2_receive(usb_dev, PYRA_COMMAND_PROFILE_SETTINGS,
69 buf, PYRA_SIZE_PROFILE_SETTINGS);
72 static int pyra_get_settings(struct usb_device *usb_dev,
73 struct pyra_settings *buf)
75 return roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
76 buf, PYRA_SIZE_SETTINGS);
79 static int pyra_set_settings(struct usb_device *usb_dev,
80 struct pyra_settings const *settings)
82 return roccat_common2_send_with_status(usb_dev,
83 PYRA_COMMAND_SETTINGS, settings,
84 PYRA_SIZE_SETTINGS);
87 static ssize_t pyra_sysfs_read(struct file *fp, struct kobject *kobj,
88 char *buf, loff_t off, size_t count,
89 size_t real_size, uint command)
91 struct device *dev =
92 container_of(kobj, struct device, kobj)->parent->parent;
93 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
94 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
95 int retval;
97 if (off >= real_size)
98 return 0;
100 if (off != 0 || count != real_size)
101 return -EINVAL;
103 mutex_lock(&pyra->pyra_lock);
104 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
105 mutex_unlock(&pyra->pyra_lock);
107 if (retval)
108 return retval;
110 return real_size;
113 static ssize_t pyra_sysfs_write(struct file *fp, struct kobject *kobj,
114 void const *buf, loff_t off, size_t count,
115 size_t real_size, uint command)
117 struct device *dev =
118 container_of(kobj, struct device, kobj)->parent->parent;
119 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
120 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
121 int retval;
123 if (off != 0 || count != real_size)
124 return -EINVAL;
126 mutex_lock(&pyra->pyra_lock);
127 retval = roccat_common2_send_with_status(usb_dev, command, (void *)buf, real_size);
128 mutex_unlock(&pyra->pyra_lock);
130 if (retval)
131 return retval;
133 return real_size;
136 #define PYRA_SYSFS_W(thingy, THINGY) \
137 static ssize_t pyra_sysfs_write_ ## thingy(struct file *fp, \
138 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
139 loff_t off, size_t count) \
141 return pyra_sysfs_write(fp, kobj, buf, off, count, \
142 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
145 #define PYRA_SYSFS_R(thingy, THINGY) \
146 static ssize_t pyra_sysfs_read_ ## thingy(struct file *fp, \
147 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
148 loff_t off, size_t count) \
150 return pyra_sysfs_read(fp, kobj, buf, off, count, \
151 PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
154 #define PYRA_SYSFS_RW(thingy, THINGY) \
155 PYRA_SYSFS_W(thingy, THINGY) \
156 PYRA_SYSFS_R(thingy, THINGY)
158 #define PYRA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
160 .attr = { .name = #thingy, .mode = 0660 }, \
161 .size = PYRA_SIZE_ ## THINGY, \
162 .read = pyra_sysfs_read_ ## thingy, \
163 .write = pyra_sysfs_write_ ## thingy \
166 #define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \
168 .attr = { .name = #thingy, .mode = 0440 }, \
169 .size = PYRA_SIZE_ ## THINGY, \
170 .read = pyra_sysfs_read_ ## thingy, \
173 #define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \
175 .attr = { .name = #thingy, .mode = 0220 }, \
176 .size = PYRA_SIZE_ ## THINGY, \
177 .write = pyra_sysfs_write_ ## thingy \
180 PYRA_SYSFS_W(control, CONTROL)
181 PYRA_SYSFS_RW(info, INFO)
182 PYRA_SYSFS_RW(profile_settings, PROFILE_SETTINGS)
183 PYRA_SYSFS_RW(profile_buttons, PROFILE_BUTTONS)
184 PYRA_SYSFS_R(settings, SETTINGS)
186 static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
187 struct kobject *kobj, struct bin_attribute *attr, char *buf,
188 loff_t off, size_t count)
190 struct device *dev =
191 container_of(kobj, struct device, kobj)->parent->parent;
192 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
193 ssize_t retval;
195 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
196 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
197 if (retval)
198 return retval;
200 return pyra_sysfs_read(fp, kobj, buf, off, count,
201 PYRA_SIZE_PROFILE_SETTINGS,
202 PYRA_COMMAND_PROFILE_SETTINGS);
205 static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
206 struct kobject *kobj, struct bin_attribute *attr, char *buf,
207 loff_t off, size_t count)
209 struct device *dev =
210 container_of(kobj, struct device, kobj)->parent->parent;
211 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
212 ssize_t retval;
214 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
215 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
216 if (retval)
217 return retval;
219 return pyra_sysfs_read(fp, kobj, buf, off, count,
220 PYRA_SIZE_PROFILE_BUTTONS,
221 PYRA_COMMAND_PROFILE_BUTTONS);
224 static ssize_t pyra_sysfs_write_settings(struct file *fp,
225 struct kobject *kobj, struct bin_attribute *attr, char *buf,
226 loff_t off, size_t count)
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 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
232 int retval = 0;
233 struct pyra_roccat_report roccat_report;
234 struct pyra_settings const *settings;
236 if (off != 0 || count != PYRA_SIZE_SETTINGS)
237 return -EINVAL;
239 mutex_lock(&pyra->pyra_lock);
241 settings = (struct pyra_settings const *)buf;
243 retval = pyra_set_settings(usb_dev, settings);
244 if (retval) {
245 mutex_unlock(&pyra->pyra_lock);
246 return retval;
249 profile_activated(pyra, settings->startup_profile);
251 roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
252 roccat_report.value = settings->startup_profile + 1;
253 roccat_report.key = 0;
254 roccat_report_event(pyra->chrdev_minor,
255 (uint8_t const *)&roccat_report);
257 mutex_unlock(&pyra->pyra_lock);
258 return PYRA_SIZE_SETTINGS;
262 static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
263 struct device_attribute *attr, char *buf)
265 struct pyra_device *pyra =
266 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
267 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
270 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
271 struct device_attribute *attr, char *buf)
273 struct pyra_device *pyra =
274 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
275 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
276 struct pyra_settings settings;
278 mutex_lock(&pyra->pyra_lock);
279 roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
280 &settings, PYRA_SIZE_SETTINGS);
281 mutex_unlock(&pyra->pyra_lock);
283 return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile);
286 static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
287 struct device_attribute *attr, char *buf)
289 struct pyra_device *pyra;
290 struct usb_device *usb_dev;
291 struct pyra_info info;
293 dev = dev->parent->parent;
294 pyra = hid_get_drvdata(dev_get_drvdata(dev));
295 usb_dev = interface_to_usbdev(to_usb_interface(dev));
297 mutex_lock(&pyra->pyra_lock);
298 roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
299 &info, PYRA_SIZE_INFO);
300 mutex_unlock(&pyra->pyra_lock);
302 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
305 static struct device_attribute pyra_attributes[] = {
306 __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
307 __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
308 __ATTR(firmware_version, 0440,
309 pyra_sysfs_show_firmware_version, NULL),
310 __ATTR(startup_profile, 0440,
311 pyra_sysfs_show_actual_profile, NULL),
312 __ATTR_NULL
315 static struct bin_attribute pyra_bin_attributes[] = {
316 PYRA_BIN_ATTRIBUTE_W(control, CONTROL),
317 PYRA_BIN_ATTRIBUTE_RW(info, INFO),
318 PYRA_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS),
319 PYRA_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS),
320 PYRA_BIN_ATTRIBUTE_RW(settings, SETTINGS),
322 .attr = { .name = "profile1_settings", .mode = 0440 },
323 .size = PYRA_SIZE_PROFILE_SETTINGS,
324 .read = pyra_sysfs_read_profilex_settings,
325 .private = &profile_numbers[0]
328 .attr = { .name = "profile2_settings", .mode = 0440 },
329 .size = PYRA_SIZE_PROFILE_SETTINGS,
330 .read = pyra_sysfs_read_profilex_settings,
331 .private = &profile_numbers[1]
334 .attr = { .name = "profile3_settings", .mode = 0440 },
335 .size = PYRA_SIZE_PROFILE_SETTINGS,
336 .read = pyra_sysfs_read_profilex_settings,
337 .private = &profile_numbers[2]
340 .attr = { .name = "profile4_settings", .mode = 0440 },
341 .size = PYRA_SIZE_PROFILE_SETTINGS,
342 .read = pyra_sysfs_read_profilex_settings,
343 .private = &profile_numbers[3]
346 .attr = { .name = "profile5_settings", .mode = 0440 },
347 .size = PYRA_SIZE_PROFILE_SETTINGS,
348 .read = pyra_sysfs_read_profilex_settings,
349 .private = &profile_numbers[4]
352 .attr = { .name = "profile1_buttons", .mode = 0440 },
353 .size = PYRA_SIZE_PROFILE_BUTTONS,
354 .read = pyra_sysfs_read_profilex_buttons,
355 .private = &profile_numbers[0]
358 .attr = { .name = "profile2_buttons", .mode = 0440 },
359 .size = PYRA_SIZE_PROFILE_BUTTONS,
360 .read = pyra_sysfs_read_profilex_buttons,
361 .private = &profile_numbers[1]
364 .attr = { .name = "profile3_buttons", .mode = 0440 },
365 .size = PYRA_SIZE_PROFILE_BUTTONS,
366 .read = pyra_sysfs_read_profilex_buttons,
367 .private = &profile_numbers[2]
370 .attr = { .name = "profile4_buttons", .mode = 0440 },
371 .size = PYRA_SIZE_PROFILE_BUTTONS,
372 .read = pyra_sysfs_read_profilex_buttons,
373 .private = &profile_numbers[3]
376 .attr = { .name = "profile5_buttons", .mode = 0440 },
377 .size = PYRA_SIZE_PROFILE_BUTTONS,
378 .read = pyra_sysfs_read_profilex_buttons,
379 .private = &profile_numbers[4]
381 __ATTR_NULL
384 static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
385 struct pyra_device *pyra)
387 struct pyra_settings settings;
388 int retval, i;
390 mutex_init(&pyra->pyra_lock);
392 retval = pyra_get_settings(usb_dev, &settings);
393 if (retval)
394 return retval;
396 for (i = 0; i < 5; ++i) {
397 retval = pyra_get_profile_settings(usb_dev,
398 &pyra->profile_settings[i], i);
399 if (retval)
400 return retval;
403 profile_activated(pyra, settings.startup_profile);
405 return 0;
408 static int pyra_init_specials(struct hid_device *hdev)
410 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
411 struct usb_device *usb_dev = interface_to_usbdev(intf);
412 struct pyra_device *pyra;
413 int retval;
415 if (intf->cur_altsetting->desc.bInterfaceProtocol
416 == USB_INTERFACE_PROTOCOL_MOUSE) {
418 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
419 if (!pyra) {
420 hid_err(hdev, "can't alloc device descriptor\n");
421 return -ENOMEM;
423 hid_set_drvdata(hdev, pyra);
425 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
426 if (retval) {
427 hid_err(hdev, "couldn't init struct pyra_device\n");
428 goto exit_free;
431 retval = roccat_connect(pyra_class, hdev,
432 sizeof(struct pyra_roccat_report));
433 if (retval < 0) {
434 hid_err(hdev, "couldn't init char dev\n");
435 } else {
436 pyra->chrdev_minor = retval;
437 pyra->roccat_claimed = 1;
439 } else {
440 hid_set_drvdata(hdev, NULL);
443 return 0;
444 exit_free:
445 kfree(pyra);
446 return retval;
449 static void pyra_remove_specials(struct hid_device *hdev)
451 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
452 struct pyra_device *pyra;
454 if (intf->cur_altsetting->desc.bInterfaceProtocol
455 == USB_INTERFACE_PROTOCOL_MOUSE) {
456 pyra = hid_get_drvdata(hdev);
457 if (pyra->roccat_claimed)
458 roccat_disconnect(pyra->chrdev_minor);
459 kfree(hid_get_drvdata(hdev));
463 static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
465 int retval;
467 retval = hid_parse(hdev);
468 if (retval) {
469 hid_err(hdev, "parse failed\n");
470 goto exit;
473 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
474 if (retval) {
475 hid_err(hdev, "hw start failed\n");
476 goto exit;
479 retval = pyra_init_specials(hdev);
480 if (retval) {
481 hid_err(hdev, "couldn't install mouse\n");
482 goto exit_stop;
484 return 0;
486 exit_stop:
487 hid_hw_stop(hdev);
488 exit:
489 return retval;
492 static void pyra_remove(struct hid_device *hdev)
494 pyra_remove_specials(hdev);
495 hid_hw_stop(hdev);
498 static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
499 u8 const *data)
501 struct pyra_mouse_event_button const *button_event;
503 switch (data[0]) {
504 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
505 button_event = (struct pyra_mouse_event_button const *)data;
506 switch (button_event->type) {
507 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
508 profile_activated(pyra, button_event->data1 - 1);
509 break;
510 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
511 pyra->actual_cpi = button_event->data1;
512 break;
514 break;
518 static void pyra_report_to_chrdev(struct pyra_device const *pyra,
519 u8 const *data)
521 struct pyra_roccat_report roccat_report;
522 struct pyra_mouse_event_button const *button_event;
524 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
525 return;
527 button_event = (struct pyra_mouse_event_button const *)data;
529 switch (button_event->type) {
530 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
531 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
532 roccat_report.type = button_event->type;
533 roccat_report.value = button_event->data1;
534 roccat_report.key = 0;
535 roccat_report_event(pyra->chrdev_minor,
536 (uint8_t const *)&roccat_report);
537 break;
538 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
539 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
540 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
541 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
542 roccat_report.type = button_event->type;
543 roccat_report.key = button_event->data1;
545 * pyra reports profile numbers with range 1-5.
546 * Keeping this behaviour.
548 roccat_report.value = pyra->actual_profile + 1;
549 roccat_report_event(pyra->chrdev_minor,
550 (uint8_t const *)&roccat_report);
552 break;
556 static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
557 u8 *data, int size)
559 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
560 struct pyra_device *pyra = hid_get_drvdata(hdev);
562 if (intf->cur_altsetting->desc.bInterfaceProtocol
563 != USB_INTERFACE_PROTOCOL_MOUSE)
564 return 0;
566 if (pyra == NULL)
567 return 0;
569 pyra_keep_values_up_to_date(pyra, data);
571 if (pyra->roccat_claimed)
572 pyra_report_to_chrdev(pyra, data);
574 return 0;
577 static const struct hid_device_id pyra_devices[] = {
578 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
579 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
580 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
581 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
585 MODULE_DEVICE_TABLE(hid, pyra_devices);
587 static struct hid_driver pyra_driver = {
588 .name = "pyra",
589 .id_table = pyra_devices,
590 .probe = pyra_probe,
591 .remove = pyra_remove,
592 .raw_event = pyra_raw_event
595 static int __init pyra_init(void)
597 int retval;
599 /* class name has to be same as driver name */
600 pyra_class = class_create(THIS_MODULE, "pyra");
601 if (IS_ERR(pyra_class))
602 return PTR_ERR(pyra_class);
603 pyra_class->dev_attrs = pyra_attributes;
604 pyra_class->dev_bin_attrs = pyra_bin_attributes;
606 retval = hid_register_driver(&pyra_driver);
607 if (retval)
608 class_destroy(pyra_class);
609 return retval;
612 static void __exit pyra_exit(void)
614 hid_unregister_driver(&pyra_driver);
615 class_destroy(pyra_class);
618 module_init(pyra_init);
619 module_exit(pyra_exit);
621 MODULE_AUTHOR("Stefan Achatz");
622 MODULE_DESCRIPTION("USB Roccat Pyra driver");
623 MODULE_LICENSE("GPL v2");