scsi: use per-cpu buffer for formatting scsi_print_result()
[linux/fpc-iii.git] / drivers / hid / hid-roccat-pyra.c
blob1a07e07d99a06c8972a2d80b8fefa8aa4f4b3848
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) \
159 PYRA_SYSFS_RW(thingy, THINGY); \
160 static struct bin_attribute bin_attr_##thingy = { \
161 .attr = { .name = #thingy, .mode = 0660 }, \
162 .size = PYRA_SIZE_ ## THINGY, \
163 .read = pyra_sysfs_read_ ## thingy, \
164 .write = pyra_sysfs_write_ ## thingy \
167 #define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \
168 PYRA_SYSFS_R(thingy, THINGY); \
169 static struct bin_attribute bin_attr_##thingy = { \
170 .attr = { .name = #thingy, .mode = 0440 }, \
171 .size = PYRA_SIZE_ ## THINGY, \
172 .read = pyra_sysfs_read_ ## thingy, \
175 #define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \
176 PYRA_SYSFS_W(thingy, THINGY); \
177 static struct bin_attribute bin_attr_##thingy = { \
178 .attr = { .name = #thingy, .mode = 0220 }, \
179 .size = PYRA_SIZE_ ## THINGY, \
180 .write = pyra_sysfs_write_ ## thingy \
183 PYRA_BIN_ATTRIBUTE_W(control, CONTROL);
184 PYRA_BIN_ATTRIBUTE_RW(info, INFO);
185 PYRA_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
186 PYRA_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
188 static ssize_t pyra_sysfs_read_profilex_settings(struct file *fp,
189 struct kobject *kobj, struct bin_attribute *attr, char *buf,
190 loff_t off, size_t count)
192 struct device *dev =
193 container_of(kobj, struct device, kobj)->parent->parent;
194 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
195 ssize_t retval;
197 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
198 PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
199 if (retval)
200 return retval;
202 return pyra_sysfs_read(fp, kobj, buf, off, count,
203 PYRA_SIZE_PROFILE_SETTINGS,
204 PYRA_COMMAND_PROFILE_SETTINGS);
207 static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
208 struct kobject *kobj, struct bin_attribute *attr, char *buf,
209 loff_t off, size_t count)
211 struct device *dev =
212 container_of(kobj, struct device, kobj)->parent->parent;
213 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
214 ssize_t retval;
216 retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
217 PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
218 if (retval)
219 return retval;
221 return pyra_sysfs_read(fp, kobj, buf, off, count,
222 PYRA_SIZE_PROFILE_BUTTONS,
223 PYRA_COMMAND_PROFILE_BUTTONS);
226 #define PROFILE_ATTR(number) \
227 static struct bin_attribute bin_attr_profile##number##_settings = { \
228 .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
229 .size = PYRA_SIZE_PROFILE_SETTINGS, \
230 .read = pyra_sysfs_read_profilex_settings, \
231 .private = &profile_numbers[number-1], \
232 }; \
233 static struct bin_attribute bin_attr_profile##number##_buttons = { \
234 .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
235 .size = PYRA_SIZE_PROFILE_BUTTONS, \
236 .read = pyra_sysfs_read_profilex_buttons, \
237 .private = &profile_numbers[number-1], \
239 PROFILE_ATTR(1);
240 PROFILE_ATTR(2);
241 PROFILE_ATTR(3);
242 PROFILE_ATTR(4);
243 PROFILE_ATTR(5);
245 static ssize_t pyra_sysfs_write_settings(struct file *fp,
246 struct kobject *kobj, struct bin_attribute *attr, char *buf,
247 loff_t off, size_t count)
249 struct device *dev =
250 container_of(kobj, struct device, kobj)->parent->parent;
251 struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
252 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
253 int retval = 0;
254 struct pyra_roccat_report roccat_report;
255 struct pyra_settings const *settings;
257 if (off != 0 || count != PYRA_SIZE_SETTINGS)
258 return -EINVAL;
260 mutex_lock(&pyra->pyra_lock);
262 settings = (struct pyra_settings const *)buf;
264 retval = pyra_set_settings(usb_dev, settings);
265 if (retval) {
266 mutex_unlock(&pyra->pyra_lock);
267 return retval;
270 profile_activated(pyra, settings->startup_profile);
272 roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
273 roccat_report.value = settings->startup_profile + 1;
274 roccat_report.key = 0;
275 roccat_report_event(pyra->chrdev_minor,
276 (uint8_t const *)&roccat_report);
278 mutex_unlock(&pyra->pyra_lock);
279 return PYRA_SIZE_SETTINGS;
282 PYRA_SYSFS_R(settings, SETTINGS);
283 static struct bin_attribute bin_attr_settings =
284 __BIN_ATTR(settings, (S_IWUSR | S_IRUGO),
285 pyra_sysfs_read_settings, pyra_sysfs_write_settings,
286 PYRA_SIZE_SETTINGS);
288 static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
289 struct device_attribute *attr, char *buf)
291 struct pyra_device *pyra =
292 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
293 return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
295 static DEVICE_ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL);
297 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
298 struct device_attribute *attr, char *buf)
300 struct pyra_device *pyra =
301 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
302 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
303 struct pyra_settings settings;
305 mutex_lock(&pyra->pyra_lock);
306 roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
307 &settings, PYRA_SIZE_SETTINGS);
308 mutex_unlock(&pyra->pyra_lock);
310 return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile);
312 static DEVICE_ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
313 static DEVICE_ATTR(startup_profile, 0440, pyra_sysfs_show_actual_profile, NULL);
315 static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
316 struct device_attribute *attr, char *buf)
318 struct pyra_device *pyra;
319 struct usb_device *usb_dev;
320 struct pyra_info info;
322 dev = dev->parent->parent;
323 pyra = hid_get_drvdata(dev_get_drvdata(dev));
324 usb_dev = interface_to_usbdev(to_usb_interface(dev));
326 mutex_lock(&pyra->pyra_lock);
327 roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
328 &info, PYRA_SIZE_INFO);
329 mutex_unlock(&pyra->pyra_lock);
331 return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
333 static DEVICE_ATTR(firmware_version, 0440, pyra_sysfs_show_firmware_version,
334 NULL);
336 static struct attribute *pyra_attrs[] = {
337 &dev_attr_actual_cpi.attr,
338 &dev_attr_actual_profile.attr,
339 &dev_attr_firmware_version.attr,
340 &dev_attr_startup_profile.attr,
341 NULL,
344 static struct bin_attribute *pyra_bin_attributes[] = {
345 &bin_attr_control,
346 &bin_attr_info,
347 &bin_attr_profile_settings,
348 &bin_attr_profile_buttons,
349 &bin_attr_settings,
350 &bin_attr_profile1_settings,
351 &bin_attr_profile2_settings,
352 &bin_attr_profile3_settings,
353 &bin_attr_profile4_settings,
354 &bin_attr_profile5_settings,
355 &bin_attr_profile1_buttons,
356 &bin_attr_profile2_buttons,
357 &bin_attr_profile3_buttons,
358 &bin_attr_profile4_buttons,
359 &bin_attr_profile5_buttons,
360 NULL,
363 static const struct attribute_group pyra_group = {
364 .attrs = pyra_attrs,
365 .bin_attrs = pyra_bin_attributes,
368 static const struct attribute_group *pyra_groups[] = {
369 &pyra_group,
370 NULL,
373 static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
374 struct pyra_device *pyra)
376 struct pyra_settings settings;
377 int retval, i;
379 mutex_init(&pyra->pyra_lock);
381 retval = pyra_get_settings(usb_dev, &settings);
382 if (retval)
383 return retval;
385 for (i = 0; i < 5; ++i) {
386 retval = pyra_get_profile_settings(usb_dev,
387 &pyra->profile_settings[i], i);
388 if (retval)
389 return retval;
392 profile_activated(pyra, settings.startup_profile);
394 return 0;
397 static int pyra_init_specials(struct hid_device *hdev)
399 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
400 struct usb_device *usb_dev = interface_to_usbdev(intf);
401 struct pyra_device *pyra;
402 int retval;
404 if (intf->cur_altsetting->desc.bInterfaceProtocol
405 == USB_INTERFACE_PROTOCOL_MOUSE) {
407 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
408 if (!pyra) {
409 hid_err(hdev, "can't alloc device descriptor\n");
410 return -ENOMEM;
412 hid_set_drvdata(hdev, pyra);
414 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
415 if (retval) {
416 hid_err(hdev, "couldn't init struct pyra_device\n");
417 goto exit_free;
420 retval = roccat_connect(pyra_class, hdev,
421 sizeof(struct pyra_roccat_report));
422 if (retval < 0) {
423 hid_err(hdev, "couldn't init char dev\n");
424 } else {
425 pyra->chrdev_minor = retval;
426 pyra->roccat_claimed = 1;
428 } else {
429 hid_set_drvdata(hdev, NULL);
432 return 0;
433 exit_free:
434 kfree(pyra);
435 return retval;
438 static void pyra_remove_specials(struct hid_device *hdev)
440 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
441 struct pyra_device *pyra;
443 if (intf->cur_altsetting->desc.bInterfaceProtocol
444 == USB_INTERFACE_PROTOCOL_MOUSE) {
445 pyra = hid_get_drvdata(hdev);
446 if (pyra->roccat_claimed)
447 roccat_disconnect(pyra->chrdev_minor);
448 kfree(hid_get_drvdata(hdev));
452 static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
454 int retval;
456 retval = hid_parse(hdev);
457 if (retval) {
458 hid_err(hdev, "parse failed\n");
459 goto exit;
462 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
463 if (retval) {
464 hid_err(hdev, "hw start failed\n");
465 goto exit;
468 retval = pyra_init_specials(hdev);
469 if (retval) {
470 hid_err(hdev, "couldn't install mouse\n");
471 goto exit_stop;
473 return 0;
475 exit_stop:
476 hid_hw_stop(hdev);
477 exit:
478 return retval;
481 static void pyra_remove(struct hid_device *hdev)
483 pyra_remove_specials(hdev);
484 hid_hw_stop(hdev);
487 static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
488 u8 const *data)
490 struct pyra_mouse_event_button const *button_event;
492 switch (data[0]) {
493 case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
494 button_event = (struct pyra_mouse_event_button const *)data;
495 switch (button_event->type) {
496 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
497 profile_activated(pyra, button_event->data1 - 1);
498 break;
499 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
500 pyra->actual_cpi = button_event->data1;
501 break;
503 break;
507 static void pyra_report_to_chrdev(struct pyra_device const *pyra,
508 u8 const *data)
510 struct pyra_roccat_report roccat_report;
511 struct pyra_mouse_event_button const *button_event;
513 if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
514 return;
516 button_event = (struct pyra_mouse_event_button const *)data;
518 switch (button_event->type) {
519 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
520 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
521 roccat_report.type = button_event->type;
522 roccat_report.value = button_event->data1;
523 roccat_report.key = 0;
524 roccat_report_event(pyra->chrdev_minor,
525 (uint8_t const *)&roccat_report);
526 break;
527 case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
528 case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
529 case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
530 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
531 roccat_report.type = button_event->type;
532 roccat_report.key = button_event->data1;
534 * pyra reports profile numbers with range 1-5.
535 * Keeping this behaviour.
537 roccat_report.value = pyra->actual_profile + 1;
538 roccat_report_event(pyra->chrdev_minor,
539 (uint8_t const *)&roccat_report);
541 break;
545 static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
546 u8 *data, int size)
548 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
549 struct pyra_device *pyra = hid_get_drvdata(hdev);
551 if (intf->cur_altsetting->desc.bInterfaceProtocol
552 != USB_INTERFACE_PROTOCOL_MOUSE)
553 return 0;
555 if (pyra == NULL)
556 return 0;
558 pyra_keep_values_up_to_date(pyra, data);
560 if (pyra->roccat_claimed)
561 pyra_report_to_chrdev(pyra, data);
563 return 0;
566 static const struct hid_device_id pyra_devices[] = {
567 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
568 USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
569 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
570 USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
574 MODULE_DEVICE_TABLE(hid, pyra_devices);
576 static struct hid_driver pyra_driver = {
577 .name = "pyra",
578 .id_table = pyra_devices,
579 .probe = pyra_probe,
580 .remove = pyra_remove,
581 .raw_event = pyra_raw_event
584 static int __init pyra_init(void)
586 int retval;
588 /* class name has to be same as driver name */
589 pyra_class = class_create(THIS_MODULE, "pyra");
590 if (IS_ERR(pyra_class))
591 return PTR_ERR(pyra_class);
592 pyra_class->dev_groups = pyra_groups;
594 retval = hid_register_driver(&pyra_driver);
595 if (retval)
596 class_destroy(pyra_class);
597 return retval;
600 static void __exit pyra_exit(void)
602 hid_unregister_driver(&pyra_driver);
603 class_destroy(pyra_class);
606 module_init(pyra_init);
607 module_exit(pyra_exit);
609 MODULE_AUTHOR("Stefan Achatz");
610 MODULE_DESCRIPTION("USB Roccat Pyra driver");
611 MODULE_LICENSE("GPL v2");