memcg: remove impossible conditional when committing
[linux-2.6.git] / drivers / hid / hid-roccat-pyra.c
blob160f481344f6673e22be30bf4232b89feac278ac
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 pyra_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 = PYRA_COMMAND_CONTROL;
53 control.value = value;
54 control.request = request;
56 return roccat_common_send(usb_dev, PYRA_USB_COMMAND_CONTROL,
57 &control, sizeof(struct pyra_control));
60 static int pyra_receive_control_status(struct usb_device *usb_dev)
62 int retval;
63 struct pyra_control control;
65 do {
66 msleep(10);
67 retval = roccat_common_receive(usb_dev, PYRA_USB_COMMAND_CONTROL,
68 &control, sizeof(struct pyra_control));
70 /* requested too early, try again */
71 } while (retval == -EPROTO);
73 if (!retval && control.command == PYRA_COMMAND_CONTROL &&
74 control.request == PYRA_CONTROL_REQUEST_STATUS &&
75 control.value == 1)
76 return 0;
77 else {
78 hid_err(usb_dev, "receive control status: unknown response 0x%x 0x%x\n",
79 control.request, control.value);
80 return retval ? retval : -EINVAL;
84 static int pyra_get_profile_settings(struct usb_device *usb_dev,
85 struct pyra_profile_settings *buf, int number)
87 int retval;
88 retval = pyra_send_control(usb_dev, number,
89 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
90 if (retval)
91 return retval;
92 return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_PROFILE_SETTINGS,
93 buf, sizeof(struct pyra_profile_settings));
96 static int pyra_get_profile_buttons(struct usb_device *usb_dev,
97 struct pyra_profile_buttons *buf, int number)
99 int retval;
100 retval = pyra_send_control(usb_dev, number,
101 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
102 if (retval)
103 return retval;
104 return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_PROFILE_BUTTONS,
105 buf, sizeof(struct pyra_profile_buttons));
108 static int pyra_get_settings(struct usb_device *usb_dev,
109 struct pyra_settings *buf)
111 return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_SETTINGS,
112 buf, sizeof(struct pyra_settings));
115 static int pyra_get_info(struct usb_device *usb_dev, struct pyra_info *buf)
117 return roccat_common_receive(usb_dev, PYRA_USB_COMMAND_INFO,
118 buf, sizeof(struct pyra_info));
121 static int pyra_send(struct usb_device *usb_dev, uint command,
122 void const *buf, uint size)
124 int retval;
125 retval = roccat_common_send(usb_dev, command, buf, size);
126 if (retval)
127 return retval;
128 return pyra_receive_control_status(usb_dev);
131 static int pyra_set_profile_settings(struct usb_device *usb_dev,
132 struct pyra_profile_settings const *settings)
134 return pyra_send(usb_dev, PYRA_USB_COMMAND_PROFILE_SETTINGS, settings,
135 sizeof(struct pyra_profile_settings));
138 static int pyra_set_profile_buttons(struct usb_device *usb_dev,
139 struct pyra_profile_buttons const *buttons)
141 return pyra_send(usb_dev, PYRA_USB_COMMAND_PROFILE_BUTTONS, buttons,
142 sizeof(struct pyra_profile_buttons));
145 static int pyra_set_settings(struct usb_device *usb_dev,
146 struct pyra_settings const *settings)
148 int retval;
149 retval = roccat_common_send(usb_dev, PYRA_USB_COMMAND_SETTINGS, settings,
150 sizeof(struct pyra_settings));
151 if (retval)
152 return retval;
153 return pyra_receive_control_status(usb_dev);
156 static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
157 struct kobject *kobj, struct bin_attribute *attr, char *buf,
158 loff_t off, size_t count)
160 struct device *dev =
161 container_of(kobj, struct device, kobj)->parent->parent;
162 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
164 if (off >= sizeof(struct pyra_profile_settings))
165 return 0;
167 if (off + count > sizeof(struct pyra_profile_settings))
168 count = sizeof(struct pyra_profile_settings) - off;
170 mutex_lock(&pyra->pyra_lock);
171 memcpy(buf, ((char const *)&pyra->profile_settings[*(uint *)(attr->private)]) + off,
172 count);
173 mutex_unlock(&pyra->pyra_lock);
175 return count;
178 static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
179 struct kobject *kobj, struct bin_attribute *attr, char *buf,
180 loff_t off, size_t count)
182 struct device *dev =
183 container_of(kobj, struct device, kobj)->parent->parent;
184 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
186 if (off >= sizeof(struct pyra_profile_buttons))
187 return 0;
189 if (off + count > sizeof(struct pyra_profile_buttons))
190 count = sizeof(struct pyra_profile_buttons) - off;
192 mutex_lock(&pyra->pyra_lock);
193 memcpy(buf, ((char const *)&pyra->profile_buttons[*(uint *)(attr->private)]) + off,
194 count);
195 mutex_unlock(&pyra->pyra_lock);
197 return count;
200 static ssize_t pyra_sysfs_write_profile_settings(struct file *fp,
201 struct kobject *kobj, struct bin_attribute *attr, char *buf,
202 loff_t off, size_t count)
204 struct device *dev =
205 container_of(kobj, struct device, kobj)->parent->parent;
206 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
207 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
208 int retval = 0;
209 int difference;
210 int profile_number;
211 struct pyra_profile_settings *profile_settings;
213 if (off != 0 || count != sizeof(struct pyra_profile_settings))
214 return -EINVAL;
216 profile_number = ((struct pyra_profile_settings const *)buf)->number;
217 profile_settings = &pyra->profile_settings[profile_number];
219 mutex_lock(&pyra->pyra_lock);
220 difference = memcmp(buf, profile_settings,
221 sizeof(struct pyra_profile_settings));
222 if (difference) {
223 retval = pyra_set_profile_settings(usb_dev,
224 (struct pyra_profile_settings const *)buf);
225 if (!retval)
226 memcpy(profile_settings, buf,
227 sizeof(struct pyra_profile_settings));
229 mutex_unlock(&pyra->pyra_lock);
231 if (retval)
232 return retval;
234 return sizeof(struct pyra_profile_settings);
237 static ssize_t pyra_sysfs_write_profile_buttons(struct file *fp,
238 struct kobject *kobj, struct bin_attribute *attr, char *buf,
239 loff_t off, size_t count)
241 struct device *dev =
242 container_of(kobj, struct device, kobj)->parent->parent;
243 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
244 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
245 int retval = 0;
246 int difference;
247 int profile_number;
248 struct pyra_profile_buttons *profile_buttons;
250 if (off != 0 || count != sizeof(struct pyra_profile_buttons))
251 return -EINVAL;
253 profile_number = ((struct pyra_profile_buttons const *)buf)->number;
254 profile_buttons = &pyra->profile_buttons[profile_number];
256 mutex_lock(&pyra->pyra_lock);
257 difference = memcmp(buf, profile_buttons,
258 sizeof(struct pyra_profile_buttons));
259 if (difference) {
260 retval = pyra_set_profile_buttons(usb_dev,
261 (struct pyra_profile_buttons const *)buf);
262 if (!retval)
263 memcpy(profile_buttons, buf,
264 sizeof(struct pyra_profile_buttons));
266 mutex_unlock(&pyra->pyra_lock);
268 if (retval)
269 return retval;
271 return sizeof(struct pyra_profile_buttons);
274 static ssize_t pyra_sysfs_read_settings(struct file *fp,
275 struct kobject *kobj, struct bin_attribute *attr, char *buf,
276 loff_t off, size_t count)
278 struct device *dev =
279 container_of(kobj, struct device, kobj)->parent->parent;
280 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
282 if (off >= sizeof(struct pyra_settings))
283 return 0;
285 if (off + count > sizeof(struct pyra_settings))
286 count = sizeof(struct pyra_settings) - off;
288 mutex_lock(&pyra->pyra_lock);
289 memcpy(buf, ((char const *)&pyra->settings) + off, count);
290 mutex_unlock(&pyra->pyra_lock);
292 return count;
295 static ssize_t pyra_sysfs_write_settings(struct file *fp,
296 struct kobject *kobj, struct bin_attribute *attr, char *buf,
297 loff_t off, size_t count)
299 struct device *dev =
300 container_of(kobj, struct device, kobj)->parent->parent;
301 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
302 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
303 int retval = 0;
304 int difference;
306 if (off != 0 || count != sizeof(struct pyra_settings))
307 return -EINVAL;
309 mutex_lock(&pyra->pyra_lock);
310 difference = memcmp(buf, &pyra->settings, sizeof(struct pyra_settings));
311 if (difference) {
312 retval = pyra_set_settings(usb_dev,
313 (struct pyra_settings const *)buf);
314 if (!retval)
315 memcpy(&pyra->settings, buf,
316 sizeof(struct pyra_settings));
318 mutex_unlock(&pyra->pyra_lock);
320 if (retval)
321 return retval;
323 profile_activated(pyra, pyra->settings.startup_profile);
325 return sizeof(struct pyra_settings);
329 static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
330 struct device_attribute *attr, char *buf)
332 struct pyra_device *pyra =
333 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
334 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
337 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
338 struct device_attribute *attr, char *buf)
340 struct pyra_device *pyra =
341 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
342 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_profile);
345 static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
346 struct device_attribute *attr, char *buf)
348 struct pyra_device *pyra =
349 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
350 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->firmware_version);
353 static ssize_t pyra_sysfs_show_startup_profile(struct device *dev,
354 struct device_attribute *attr, char *buf)
356 struct pyra_device *pyra =
357 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
358 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->settings.startup_profile);
361 static struct device_attribute pyra_attributes[] = {
362 __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
363 __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
364 __ATTR(firmware_version, 0440,
365 pyra_sysfs_show_firmware_version, NULL),
366 __ATTR(startup_profile, 0440,
367 pyra_sysfs_show_startup_profile, NULL),
368 __ATTR_NULL
371 static struct bin_attribute pyra_bin_attributes[] = {
373 .attr = { .name = "profile_settings", .mode = 0220 },
374 .size = sizeof(struct pyra_profile_settings),
375 .write = pyra_sysfs_write_profile_settings
378 .attr = { .name = "profile1_settings", .mode = 0440 },
379 .size = sizeof(struct pyra_profile_settings),
380 .read = pyra_sysfs_read_profilex_settings,
381 .private = &profile_numbers[0]
384 .attr = { .name = "profile2_settings", .mode = 0440 },
385 .size = sizeof(struct pyra_profile_settings),
386 .read = pyra_sysfs_read_profilex_settings,
387 .private = &profile_numbers[1]
390 .attr = { .name = "profile3_settings", .mode = 0440 },
391 .size = sizeof(struct pyra_profile_settings),
392 .read = pyra_sysfs_read_profilex_settings,
393 .private = &profile_numbers[2]
396 .attr = { .name = "profile4_settings", .mode = 0440 },
397 .size = sizeof(struct pyra_profile_settings),
398 .read = pyra_sysfs_read_profilex_settings,
399 .private = &profile_numbers[3]
402 .attr = { .name = "profile5_settings", .mode = 0440 },
403 .size = sizeof(struct pyra_profile_settings),
404 .read = pyra_sysfs_read_profilex_settings,
405 .private = &profile_numbers[4]
408 .attr = { .name = "profile_buttons", .mode = 0220 },
409 .size = sizeof(struct pyra_profile_buttons),
410 .write = pyra_sysfs_write_profile_buttons
413 .attr = { .name = "profile1_buttons", .mode = 0440 },
414 .size = sizeof(struct pyra_profile_buttons),
415 .read = pyra_sysfs_read_profilex_buttons,
416 .private = &profile_numbers[0]
419 .attr = { .name = "profile2_buttons", .mode = 0440 },
420 .size = sizeof(struct pyra_profile_buttons),
421 .read = pyra_sysfs_read_profilex_buttons,
422 .private = &profile_numbers[1]
425 .attr = { .name = "profile3_buttons", .mode = 0440 },
426 .size = sizeof(struct pyra_profile_buttons),
427 .read = pyra_sysfs_read_profilex_buttons,
428 .private = &profile_numbers[2]
431 .attr = { .name = "profile4_buttons", .mode = 0440 },
432 .size = sizeof(struct pyra_profile_buttons),
433 .read = pyra_sysfs_read_profilex_buttons,
434 .private = &profile_numbers[3]
437 .attr = { .name = "profile5_buttons", .mode = 0440 },
438 .size = sizeof(struct pyra_profile_buttons),
439 .read = pyra_sysfs_read_profilex_buttons,
440 .private = &profile_numbers[4]
443 .attr = { .name = "settings", .mode = 0660 },
444 .size = sizeof(struct pyra_settings),
445 .read = pyra_sysfs_read_settings,
446 .write = pyra_sysfs_write_settings
448 __ATTR_NULL
451 static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
452 struct pyra_device *pyra)
454 struct pyra_info info;
455 int retval, i;
457 mutex_init(&pyra->pyra_lock);
459 retval = pyra_get_info(usb_dev, &info);
460 if (retval)
461 return retval;
463 pyra->firmware_version = info.firmware_version;
465 retval = pyra_get_settings(usb_dev, &pyra->settings);
466 if (retval)
467 return retval;
469 for (i = 0; i < 5; ++i) {
470 retval = pyra_get_profile_settings(usb_dev,
471 &pyra->profile_settings[i], i);
472 if (retval)
473 return retval;
475 retval = pyra_get_profile_buttons(usb_dev,
476 &pyra->profile_buttons[i], i);
477 if (retval)
478 return retval;
481 profile_activated(pyra, pyra->settings.startup_profile);
483 return 0;
486 static int pyra_init_specials(struct hid_device *hdev)
488 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
489 struct usb_device *usb_dev = interface_to_usbdev(intf);
490 struct pyra_device *pyra;
491 int retval;
493 if (intf->cur_altsetting->desc.bInterfaceProtocol
494 == USB_INTERFACE_PROTOCOL_MOUSE) {
496 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
497 if (!pyra) {
498 hid_err(hdev, "can't alloc device descriptor\n");
499 return -ENOMEM;
501 hid_set_drvdata(hdev, pyra);
503 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
504 if (retval) {
505 hid_err(hdev, "couldn't init struct pyra_device\n");
506 goto exit_free;
509 retval = roccat_connect(pyra_class, hdev,
510 sizeof(struct pyra_roccat_report));
511 if (retval < 0) {
512 hid_err(hdev, "couldn't init char dev\n");
513 } else {
514 pyra->chrdev_minor = retval;
515 pyra->roccat_claimed = 1;
517 } else {
518 hid_set_drvdata(hdev, NULL);
521 return 0;
522 exit_free:
523 kfree(pyra);
524 return retval;
527 static void pyra_remove_specials(struct hid_device *hdev)
529 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
530 struct pyra_device *pyra;
532 if (intf->cur_altsetting->desc.bInterfaceProtocol
533 == USB_INTERFACE_PROTOCOL_MOUSE) {
534 pyra = hid_get_drvdata(hdev);
535 if (pyra->roccat_claimed)
536 roccat_disconnect(pyra->chrdev_minor);
537 kfree(hid_get_drvdata(hdev));
541 static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
543 int retval;
545 retval = hid_parse(hdev);
546 if (retval) {
547 hid_err(hdev, "parse failed\n");
548 goto exit;
551 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
552 if (retval) {
553 hid_err(hdev, "hw start failed\n");
554 goto exit;
557 retval = pyra_init_specials(hdev);
558 if (retval) {
559 hid_err(hdev, "couldn't install mouse\n");
560 goto exit_stop;
562 return 0;
564 exit_stop:
565 hid_hw_stop(hdev);
566 exit:
567 return retval;
570 static void pyra_remove(struct hid_device *hdev)
572 pyra_remove_specials(hdev);
573 hid_hw_stop(hdev);
576 static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
577 u8 const *data)
579 struct pyra_mouse_event_button const *button_event;
581 switch (data[0]) {
582 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
583 button_event = (struct pyra_mouse_event_button const *)data;
584 switch (button_event->type) {
585 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
586 profile_activated(pyra, button_event->data1 - 1);
587 break;
588 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
589 pyra->actual_cpi = button_event->data1;
590 break;
592 break;
596 static void pyra_report_to_chrdev(struct pyra_device const *pyra,
597 u8 const *data)
599 struct pyra_roccat_report roccat_report;
600 struct pyra_mouse_event_button const *button_event;
602 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
603 return;
605 button_event = (struct pyra_mouse_event_button const *)data;
607 switch (button_event->type) {
608 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
609 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
610 roccat_report.type = button_event->type;
611 roccat_report.value = button_event->data1;
612 roccat_report.key = 0;
613 roccat_report_event(pyra->chrdev_minor,
614 (uint8_t const *)&roccat_report);
615 break;
616 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
617 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
618 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
619 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
620 roccat_report.type = button_event->type;
621 roccat_report.key = button_event->data1;
623 * pyra reports profile numbers with range 1-5.
624 * Keeping this behaviour.
626 roccat_report.value = pyra->actual_profile + 1;
627 roccat_report_event(pyra->chrdev_minor,
628 (uint8_t const *)&roccat_report);
630 break;
634 static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
635 u8 *data, int size)
637 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
638 struct pyra_device *pyra = hid_get_drvdata(hdev);
640 if (intf->cur_altsetting->desc.bInterfaceProtocol
641 != USB_INTERFACE_PROTOCOL_MOUSE)
642 return 0;
644 pyra_keep_values_up_to_date(pyra, data);
646 if (pyra->roccat_claimed)
647 pyra_report_to_chrdev(pyra, data);
649 return 0;
652 static const struct hid_device_id pyra_devices[] = {
653 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
654 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
655 /* TODO add USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS after testing */
659 MODULE_DEVICE_TABLE(hid, pyra_devices);
661 static struct hid_driver pyra_driver = {
662 .name = "pyra",
663 .id_table = pyra_devices,
664 .probe = pyra_probe,
665 .remove = pyra_remove,
666 .raw_event = pyra_raw_event
669 static int __init pyra_init(void)
671 int retval;
673 /* class name has to be same as driver name */
674 pyra_class = class_create(THIS_MODULE, "pyra");
675 if (IS_ERR(pyra_class))
676 return PTR_ERR(pyra_class);
677 pyra_class->dev_attrs = pyra_attributes;
678 pyra_class->dev_bin_attrs = pyra_bin_attributes;
680 retval = hid_register_driver(&pyra_driver);
681 if (retval)
682 class_destroy(pyra_class);
683 return retval;
686 static void __exit pyra_exit(void)
688 hid_unregister_driver(&pyra_driver);
689 class_destroy(pyra_class);
692 module_init(pyra_init);
693 module_exit(pyra_exit);
695 MODULE_AUTHOR("Stefan Achatz");
696 MODULE_DESCRIPTION("USB Roccat Pyra driver");
697 MODULE_LICENSE("GPL v2");