]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/hid/hid-roccat-pyra.c
HID: roccat: cleanup of pyra module
[karo-tx-linux.git] / drivers / hid / hid-roccat-pyra.c
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/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"
29
30 static uint profile_numbers[5] = {0, 1, 2, 3, 4};
31
32 /* pyra_class is used for creating sysfs attributes via roccat char device */
33 static struct class *pyra_class;
34
35 static void profile_activated(struct pyra_device *pyra,
36                 unsigned int new_profile)
37 {
38         pyra->actual_profile = new_profile;
39         pyra->actual_cpi = pyra->profile_settings[pyra->actual_profile].y_cpi;
40 }
41
42 static int pyra_send_control(struct usb_device *usb_dev, int value,
43                 enum pyra_control_requests request)
44 {
45         struct roccat_common2_control control;
46
47         if ((request == PYRA_CONTROL_REQUEST_PROFILE_SETTINGS ||
48                         request == PYRA_CONTROL_REQUEST_PROFILE_BUTTONS) &&
49                         (value < 0 || value > 4))
50                 return -EINVAL;
51
52         control.command = ROCCAT_COMMON_COMMAND_CONTROL;
53         control.value = value;
54         control.request = request;
55
56         return roccat_common2_send(usb_dev, ROCCAT_COMMON_COMMAND_CONTROL,
57                         &control, sizeof(struct roccat_common2_control));
58 }
59
60 static int pyra_get_profile_settings(struct usb_device *usb_dev,
61                 struct pyra_profile_settings *buf, int number)
62 {
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);
70 }
71
72 static int pyra_get_settings(struct usb_device *usb_dev,
73                 struct pyra_settings *buf)
74 {
75         return roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
76                         buf, PYRA_SIZE_SETTINGS);
77 }
78
79 static int pyra_set_settings(struct usb_device *usb_dev,
80                 struct pyra_settings const *settings)
81 {
82         return roccat_common2_send_with_status(usb_dev,
83                         PYRA_COMMAND_SETTINGS, settings,
84                         PYRA_SIZE_SETTINGS);
85 }
86
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)
90 {
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;
96
97         if (off >= real_size)
98                 return 0;
99
100         if (off != 0 || count != real_size)
101                 return -EINVAL;
102
103         mutex_lock(&pyra->pyra_lock);
104         retval = roccat_common2_receive(usb_dev, command, buf, real_size);
105         mutex_unlock(&pyra->pyra_lock);
106
107         if (retval)
108                 return retval;
109
110         return real_size;
111 }
112
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)
116 {
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;
122
123         if (off != 0 || count != real_size)
124                 return -EINVAL;
125
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);
129
130         if (retval)
131                 return retval;
132
133         return real_size;
134 }
135
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) \
140 { \
141         return pyra_sysfs_write(fp, kobj, buf, off, count, \
142                         PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
143 }
144
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) \
149 { \
150         return pyra_sysfs_read(fp, kobj, buf, off, count, \
151                         PYRA_SIZE_ ## THINGY, PYRA_COMMAND_ ## THINGY); \
152 }
153
154 #define PYRA_SYSFS_RW(thingy, THINGY) \
155 PYRA_SYSFS_W(thingy, THINGY) \
156 PYRA_SYSFS_R(thingy, THINGY)
157
158 #define PYRA_BIN_ATTRIBUTE_RW(thingy, THINGY) \
159 { \
160         .attr = { .name = #thingy, .mode = 0660 }, \
161         .size = PYRA_SIZE_ ## THINGY, \
162         .read = pyra_sysfs_read_ ## thingy, \
163         .write = pyra_sysfs_write_ ## thingy \
164 }
165
166 #define PYRA_BIN_ATTRIBUTE_R(thingy, THINGY) \
167 { \
168         .attr = { .name = #thingy, .mode = 0440 }, \
169         .size = PYRA_SIZE_ ## THINGY, \
170         .read = pyra_sysfs_read_ ## thingy, \
171 }
172
173 #define PYRA_BIN_ATTRIBUTE_W(thingy, THINGY) \
174 { \
175         .attr = { .name = #thingy, .mode = 0220 }, \
176         .size = PYRA_SIZE_ ## THINGY, \
177         .write = pyra_sysfs_write_ ## thingy \
178 }
179
180 PYRA_SYSFS_RW(info, INFO)
181 PYRA_SYSFS_W(profile_settings, PROFILE_SETTINGS)
182 PYRA_SYSFS_W(profile_buttons, PROFILE_BUTTONS)
183 PYRA_SYSFS_R(settings, SETTINGS)
184
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)
188 {
189         struct device *dev =
190                         container_of(kobj, struct device, kobj)->parent->parent;
191         struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
192         ssize_t retval;
193
194         retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
195                         PYRA_CONTROL_REQUEST_PROFILE_SETTINGS);
196         if (retval)
197                 return retval;
198
199         return pyra_sysfs_read(fp, kobj, buf, off, count,
200                         PYRA_SIZE_PROFILE_SETTINGS,
201                         PYRA_COMMAND_PROFILE_SETTINGS);
202 }
203
204 static ssize_t pyra_sysfs_read_profilex_buttons(struct file *fp,
205                 struct kobject *kobj, struct bin_attribute *attr, char *buf,
206                 loff_t off, size_t count)
207 {
208         struct device *dev =
209                         container_of(kobj, struct device, kobj)->parent->parent;
210         struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
211         ssize_t retval;
212
213         retval = pyra_send_control(usb_dev, *(uint *)(attr->private),
214                         PYRA_CONTROL_REQUEST_PROFILE_BUTTONS);
215         if (retval)
216                 return retval;
217
218         return pyra_sysfs_read(fp, kobj, buf, off, count,
219                         PYRA_SIZE_PROFILE_BUTTONS,
220                         PYRA_COMMAND_PROFILE_BUTTONS);
221 }
222
223 static ssize_t pyra_sysfs_write_settings(struct file *fp,
224                 struct kobject *kobj, struct bin_attribute *attr, char *buf,
225                 loff_t off, size_t count)
226 {
227         struct device *dev =
228                         container_of(kobj, struct device, kobj)->parent->parent;
229         struct pyra_device *pyra = hid_get_drvdata(dev_get_drvdata(dev));
230         struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
231         int retval = 0;
232         struct pyra_roccat_report roccat_report;
233         struct pyra_settings const *settings;
234
235         if (off != 0 || count != PYRA_SIZE_SETTINGS)
236                 return -EINVAL;
237
238         mutex_lock(&pyra->pyra_lock);
239
240         settings = (struct pyra_settings const *)buf;
241
242         retval = pyra_set_settings(usb_dev, settings);
243         if (retval) {
244                 mutex_unlock(&pyra->pyra_lock);
245                 return retval;
246         }
247
248         profile_activated(pyra, settings->startup_profile);
249
250         roccat_report.type = PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2;
251         roccat_report.value = settings->startup_profile + 1;
252         roccat_report.key = 0;
253         roccat_report_event(pyra->chrdev_minor,
254                         (uint8_t const *)&roccat_report);
255
256         mutex_unlock(&pyra->pyra_lock);
257         return PYRA_SIZE_SETTINGS;
258 }
259
260
261 static ssize_t pyra_sysfs_show_actual_cpi(struct device *dev,
262                 struct device_attribute *attr, char *buf)
263 {
264         struct pyra_device *pyra =
265                         hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
266         return snprintf(buf, PAGE_SIZE, "%d\n", pyra->actual_cpi);
267 }
268
269 static ssize_t pyra_sysfs_show_actual_profile(struct device *dev,
270                 struct device_attribute *attr, char *buf)
271 {
272         struct pyra_device *pyra =
273                         hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
274         struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
275         struct pyra_settings settings;
276
277         mutex_lock(&pyra->pyra_lock);
278         roccat_common2_receive(usb_dev, PYRA_COMMAND_SETTINGS,
279                         &settings, PYRA_SIZE_SETTINGS);
280         mutex_unlock(&pyra->pyra_lock);
281
282         return snprintf(buf, PAGE_SIZE, "%d\n", settings.startup_profile);
283 }
284
285 static ssize_t pyra_sysfs_show_firmware_version(struct device *dev,
286                 struct device_attribute *attr, char *buf)
287 {
288         struct pyra_device *pyra;
289         struct usb_device *usb_dev;
290         struct pyra_info info;
291
292         dev = dev->parent->parent;
293         pyra = hid_get_drvdata(dev_get_drvdata(dev));
294         usb_dev = interface_to_usbdev(to_usb_interface(dev));
295
296         mutex_lock(&pyra->pyra_lock);
297         roccat_common2_receive(usb_dev, PYRA_COMMAND_INFO,
298                         &info, PYRA_SIZE_INFO);
299         mutex_unlock(&pyra->pyra_lock);
300
301         return snprintf(buf, PAGE_SIZE, "%d\n", info.firmware_version);
302 }
303
304 static struct device_attribute pyra_attributes[] = {
305         __ATTR(actual_cpi, 0440, pyra_sysfs_show_actual_cpi, NULL),
306         __ATTR(actual_profile, 0440, pyra_sysfs_show_actual_profile, NULL),
307         __ATTR(firmware_version, 0440,
308                         pyra_sysfs_show_firmware_version, NULL),
309         __ATTR(startup_profile, 0440,
310                         pyra_sysfs_show_actual_profile, NULL),
311         __ATTR_NULL
312 };
313
314 static struct bin_attribute pyra_bin_attributes[] = {
315         PYRA_BIN_ATTRIBUTE_RW(info, INFO),
316         PYRA_BIN_ATTRIBUTE_W(profile_settings, PROFILE_SETTINGS),
317         PYRA_BIN_ATTRIBUTE_W(profile_buttons, PROFILE_BUTTONS),
318         PYRA_BIN_ATTRIBUTE_RW(settings, SETTINGS),
319         {
320                 .attr = { .name = "profile1_settings", .mode = 0440 },
321                 .size = PYRA_SIZE_PROFILE_SETTINGS,
322                 .read = pyra_sysfs_read_profilex_settings,
323                 .private = &profile_numbers[0]
324         },
325         {
326                 .attr = { .name = "profile2_settings", .mode = 0440 },
327                 .size = PYRA_SIZE_PROFILE_SETTINGS,
328                 .read = pyra_sysfs_read_profilex_settings,
329                 .private = &profile_numbers[1]
330         },
331         {
332                 .attr = { .name = "profile3_settings", .mode = 0440 },
333                 .size = PYRA_SIZE_PROFILE_SETTINGS,
334                 .read = pyra_sysfs_read_profilex_settings,
335                 .private = &profile_numbers[2]
336         },
337         {
338                 .attr = { .name = "profile4_settings", .mode = 0440 },
339                 .size = PYRA_SIZE_PROFILE_SETTINGS,
340                 .read = pyra_sysfs_read_profilex_settings,
341                 .private = &profile_numbers[3]
342         },
343         {
344                 .attr = { .name = "profile5_settings", .mode = 0440 },
345                 .size = PYRA_SIZE_PROFILE_SETTINGS,
346                 .read = pyra_sysfs_read_profilex_settings,
347                 .private = &profile_numbers[4]
348         },
349         {
350                 .attr = { .name = "profile1_buttons", .mode = 0440 },
351                 .size = PYRA_SIZE_PROFILE_BUTTONS,
352                 .read = pyra_sysfs_read_profilex_buttons,
353                 .private = &profile_numbers[0]
354         },
355         {
356                 .attr = { .name = "profile2_buttons", .mode = 0440 },
357                 .size = PYRA_SIZE_PROFILE_BUTTONS,
358                 .read = pyra_sysfs_read_profilex_buttons,
359                 .private = &profile_numbers[1]
360         },
361         {
362                 .attr = { .name = "profile3_buttons", .mode = 0440 },
363                 .size = PYRA_SIZE_PROFILE_BUTTONS,
364                 .read = pyra_sysfs_read_profilex_buttons,
365                 .private = &profile_numbers[2]
366         },
367         {
368                 .attr = { .name = "profile4_buttons", .mode = 0440 },
369                 .size = PYRA_SIZE_PROFILE_BUTTONS,
370                 .read = pyra_sysfs_read_profilex_buttons,
371                 .private = &profile_numbers[3]
372         },
373         {
374                 .attr = { .name = "profile5_buttons", .mode = 0440 },
375                 .size = PYRA_SIZE_PROFILE_BUTTONS,
376                 .read = pyra_sysfs_read_profilex_buttons,
377                 .private = &profile_numbers[4]
378         },
379         __ATTR_NULL
380 };
381
382 static int pyra_init_pyra_device_struct(struct usb_device *usb_dev,
383                 struct pyra_device *pyra)
384 {
385         struct pyra_settings settings;
386         int retval, i;
387
388         mutex_init(&pyra->pyra_lock);
389
390         retval = pyra_get_settings(usb_dev, &settings);
391         if (retval)
392                 return retval;
393
394         for (i = 0; i < 5; ++i) {
395                 retval = pyra_get_profile_settings(usb_dev,
396                                 &pyra->profile_settings[i], i);
397                 if (retval)
398                         return retval;
399         }
400
401         profile_activated(pyra, settings.startup_profile);
402
403         return 0;
404 }
405
406 static int pyra_init_specials(struct hid_device *hdev)
407 {
408         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
409         struct usb_device *usb_dev = interface_to_usbdev(intf);
410         struct pyra_device *pyra;
411         int retval;
412
413         if (intf->cur_altsetting->desc.bInterfaceProtocol
414                         == USB_INTERFACE_PROTOCOL_MOUSE) {
415
416                 pyra = kzalloc(sizeof(*pyra), GFP_KERNEL);
417                 if (!pyra) {
418                         hid_err(hdev, "can't alloc device descriptor\n");
419                         return -ENOMEM;
420                 }
421                 hid_set_drvdata(hdev, pyra);
422
423                 retval = pyra_init_pyra_device_struct(usb_dev, pyra);
424                 if (retval) {
425                         hid_err(hdev, "couldn't init struct pyra_device\n");
426                         goto exit_free;
427                 }
428
429                 retval = roccat_connect(pyra_class, hdev,
430                                 sizeof(struct pyra_roccat_report));
431                 if (retval < 0) {
432                         hid_err(hdev, "couldn't init char dev\n");
433                 } else {
434                         pyra->chrdev_minor = retval;
435                         pyra->roccat_claimed = 1;
436                 }
437         } else {
438                 hid_set_drvdata(hdev, NULL);
439         }
440
441         return 0;
442 exit_free:
443         kfree(pyra);
444         return retval;
445 }
446
447 static void pyra_remove_specials(struct hid_device *hdev)
448 {
449         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
450         struct pyra_device *pyra;
451
452         if (intf->cur_altsetting->desc.bInterfaceProtocol
453                         == USB_INTERFACE_PROTOCOL_MOUSE) {
454                 pyra = hid_get_drvdata(hdev);
455                 if (pyra->roccat_claimed)
456                         roccat_disconnect(pyra->chrdev_minor);
457                 kfree(hid_get_drvdata(hdev));
458         }
459 }
460
461 static int pyra_probe(struct hid_device *hdev, const struct hid_device_id *id)
462 {
463         int retval;
464
465         retval = hid_parse(hdev);
466         if (retval) {
467                 hid_err(hdev, "parse failed\n");
468                 goto exit;
469         }
470
471         retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
472         if (retval) {
473                 hid_err(hdev, "hw start failed\n");
474                 goto exit;
475         }
476
477         retval = pyra_init_specials(hdev);
478         if (retval) {
479                 hid_err(hdev, "couldn't install mouse\n");
480                 goto exit_stop;
481         }
482         return 0;
483
484 exit_stop:
485         hid_hw_stop(hdev);
486 exit:
487         return retval;
488 }
489
490 static void pyra_remove(struct hid_device *hdev)
491 {
492         pyra_remove_specials(hdev);
493         hid_hw_stop(hdev);
494 }
495
496 static void pyra_keep_values_up_to_date(struct pyra_device *pyra,
497                 u8 const *data)
498 {
499         struct pyra_mouse_event_button const *button_event;
500
501         switch (data[0]) {
502         case PYRA_MOUSE_REPORT_NUMBER_BUTTON:
503                 button_event = (struct pyra_mouse_event_button const *)data;
504                 switch (button_event->type) {
505                 case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
506                         profile_activated(pyra, button_event->data1 - 1);
507                         break;
508                 case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
509                         pyra->actual_cpi = button_event->data1;
510                         break;
511                 }
512                 break;
513         }
514 }
515
516 static void pyra_report_to_chrdev(struct pyra_device const *pyra,
517                 u8 const *data)
518 {
519         struct pyra_roccat_report roccat_report;
520         struct pyra_mouse_event_button const *button_event;
521
522         if (data[0] != PYRA_MOUSE_REPORT_NUMBER_BUTTON)
523                 return;
524
525         button_event = (struct pyra_mouse_event_button const *)data;
526
527         switch (button_event->type) {
528         case PYRA_MOUSE_EVENT_BUTTON_TYPE_PROFILE_2:
529         case PYRA_MOUSE_EVENT_BUTTON_TYPE_CPI:
530                 roccat_report.type = button_event->type;
531                 roccat_report.value = button_event->data1;
532                 roccat_report.key = 0;
533                 roccat_report_event(pyra->chrdev_minor,
534                                 (uint8_t const *)&roccat_report);
535                 break;
536         case PYRA_MOUSE_EVENT_BUTTON_TYPE_MACRO:
537         case PYRA_MOUSE_EVENT_BUTTON_TYPE_SHORTCUT:
538         case PYRA_MOUSE_EVENT_BUTTON_TYPE_QUICKLAUNCH:
539                 if (button_event->data2 == PYRA_MOUSE_EVENT_BUTTON_PRESS) {
540                         roccat_report.type = button_event->type;
541                         roccat_report.key = button_event->data1;
542                         /*
543                          * pyra reports profile numbers with range 1-5.
544                          * Keeping this behaviour.
545                          */
546                         roccat_report.value = pyra->actual_profile + 1;
547                         roccat_report_event(pyra->chrdev_minor,
548                                         (uint8_t const *)&roccat_report);
549                 }
550                 break;
551         }
552 }
553
554 static int pyra_raw_event(struct hid_device *hdev, struct hid_report *report,
555                 u8 *data, int size)
556 {
557         struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
558         struct pyra_device *pyra = hid_get_drvdata(hdev);
559
560         if (intf->cur_altsetting->desc.bInterfaceProtocol
561                         != USB_INTERFACE_PROTOCOL_MOUSE)
562                 return 0;
563
564         if (pyra == NULL)
565                 return 0;
566
567         pyra_keep_values_up_to_date(pyra, data);
568
569         if (pyra->roccat_claimed)
570                 pyra_report_to_chrdev(pyra, data);
571
572         return 0;
573 }
574
575 static const struct hid_device_id pyra_devices[] = {
576         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
577                         USB_DEVICE_ID_ROCCAT_PYRA_WIRED) },
578         { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT,
579                         USB_DEVICE_ID_ROCCAT_PYRA_WIRELESS) },
580         { }
581 };
582
583 MODULE_DEVICE_TABLE(hid, pyra_devices);
584
585 static struct hid_driver pyra_driver = {
586                 .name = "pyra",
587                 .id_table = pyra_devices,
588                 .probe = pyra_probe,
589                 .remove = pyra_remove,
590                 .raw_event = pyra_raw_event
591 };
592
593 static int __init pyra_init(void)
594 {
595         int retval;
596
597         /* class name has to be same as driver name */
598         pyra_class = class_create(THIS_MODULE, "pyra");
599         if (IS_ERR(pyra_class))
600                 return PTR_ERR(pyra_class);
601         pyra_class->dev_attrs = pyra_attributes;
602         pyra_class->dev_bin_attrs = pyra_bin_attributes;
603
604         retval = hid_register_driver(&pyra_driver);
605         if (retval)
606                 class_destroy(pyra_class);
607         return retval;
608 }
609
610 static void __exit pyra_exit(void)
611 {
612         hid_unregister_driver(&pyra_driver);
613         class_destroy(pyra_class);
614 }
615
616 module_init(pyra_init);
617 module_exit(pyra_exit);
618
619 MODULE_AUTHOR("Stefan Achatz");
620 MODULE_DESCRIPTION("USB Roccat Pyra driver");
621 MODULE_LICENSE("GPL v2");