aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2010-05-26 11:41:56 -0400
committerLinus Torvalds <torvalds@linux-foundation.org>2010-05-26 11:41:56 -0400
commitf45471cbda9df24f990154a963741c9bd4c0b0aa (patch)
tree145ed5450efae53f73ae0dca263b84688d6b97ca
parent2214482cb00e6da1397c2ecde5b392490eb9637f (diff)
parent0e927bfc87037c1f44d6b61be12c36f6215ee5b2 (diff)
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid: HID: roccat: fix build failure if built as module HID: roccat: propagate special events of roccat hardware to userspace HID: Add the GYR4101US USB ID to hid-gyration HID: fix hid-roccat-kone for bin_attr API change
-rw-r--r--drivers/hid/Kconfig8
-rw-r--r--drivers/hid/Makefile1
-rw-r--r--drivers/hid/hid-core.c1
-rw-r--r--drivers/hid/hid-gyration.c1
-rw-r--r--drivers/hid/hid-ids.h1
-rw-r--r--drivers/hid/hid-roccat-kone.c73
-rw-r--r--drivers/hid/hid-roccat-kone.h9
-rw-r--r--drivers/hid/hid-roccat.c428
-rw-r--r--drivers/hid/hid-roccat.h31
9 files changed, 541 insertions, 12 deletions
diff --git a/drivers/hid/Kconfig b/drivers/hid/Kconfig
index 76ba59b9fea..132278fa624 100644
--- a/drivers/hid/Kconfig
+++ b/drivers/hid/Kconfig
@@ -347,6 +347,14 @@ config HID_QUANTA
347 ---help--- 347 ---help---
348 Support for Quanta Optical Touch dual-touch panels. 348 Support for Quanta Optical Touch dual-touch panels.
349 349
350config HID_ROCCAT
351 tristate "Roccat special event support"
352 depends on USB_HID
353 ---help---
354 Support for Roccat special events.
355 Say Y here if you have a Roccat mouse or keyboard and want OSD or
356 macro execution support.
357
350config HID_ROCCAT_KONE 358config HID_ROCCAT_KONE
351 tristate "Roccat Kone Mouse support" 359 tristate "Roccat Kone Mouse support"
352 depends on USB_HID 360 depends on USB_HID
diff --git a/drivers/hid/Makefile b/drivers/hid/Makefile
index 22e47eaeea3..987fa062736 100644
--- a/drivers/hid/Makefile
+++ b/drivers/hid/Makefile
@@ -48,6 +48,7 @@ obj-$(CONFIG_HID_QUANTA) += hid-quanta.o
48obj-$(CONFIG_HID_PANTHERLORD) += hid-pl.o 48obj-$(CONFIG_HID_PANTHERLORD) += hid-pl.o
49obj-$(CONFIG_HID_PETALYNX) += hid-petalynx.o 49obj-$(CONFIG_HID_PETALYNX) += hid-petalynx.o
50obj-$(CONFIG_HID_PICOLCD) += hid-picolcd.o 50obj-$(CONFIG_HID_PICOLCD) += hid-picolcd.o
51obj-$(CONFIG_HID_ROCCAT) += hid-roccat.o
51obj-$(CONFIG_HID_ROCCAT_KONE) += hid-roccat-kone.o 52obj-$(CONFIG_HID_ROCCAT_KONE) += hid-roccat-kone.o
52obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o 53obj-$(CONFIG_HID_SAMSUNG) += hid-samsung.o
53obj-$(CONFIG_HID_SMARTJOYPLUS) += hid-sjoy.o 54obj-$(CONFIG_HID_SMARTJOYPLUS) += hid-sjoy.o
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index e10e314d38c..aa0f7dcabcd 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -1301,6 +1301,7 @@ static const struct hid_device_id hid_blacklist[] = {
1301 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) }, 1301 { HID_USB_DEVICE(USB_VENDOR_ID_GREENASIA, 0x0012) },
1302 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) }, 1302 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
1303 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) }, 1303 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
1304 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_3) },
1304 { HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) }, 1305 { HID_USB_DEVICE(USB_VENDOR_ID_KENSINGTON, USB_DEVICE_ID_KS_SLIMBLADE) },
1305 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) }, 1306 { HID_USB_DEVICE(USB_VENDOR_ID_KYE, USB_DEVICE_ID_KYE_ERGO_525V) },
1306 { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) }, 1307 { HID_USB_DEVICE(USB_VENDOR_ID_LABTEC, USB_DEVICE_ID_LABTEC_WIRELESS_KEYBOARD) },
diff --git a/drivers/hid/hid-gyration.c b/drivers/hid/hid-gyration.c
index 62416e6baec..3975e039c3d 100644
--- a/drivers/hid/hid-gyration.c
+++ b/drivers/hid/hid-gyration.c
@@ -73,6 +73,7 @@ static int gyration_event(struct hid_device *hdev, struct hid_field *field,
73static const struct hid_device_id gyration_devices[] = { 73static const struct hid_device_id gyration_devices[] = {
74 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) }, 74 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE) },
75 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) }, 75 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_2) },
76 { HID_USB_DEVICE(USB_VENDOR_ID_GYRATION, USB_DEVICE_ID_GYRATION_REMOTE_3) },
76 { } 77 { }
77}; 78};
78MODULE_DEVICE_TABLE(hid, gyration_devices); 79MODULE_DEVICE_TABLE(hid, gyration_devices);
diff --git a/drivers/hid/hid-ids.h b/drivers/hid/hid-ids.h
index 9776896cc4f..6af77ed0b55 100644
--- a/drivers/hid/hid-ids.h
+++ b/drivers/hid/hid-ids.h
@@ -282,6 +282,7 @@
282#define USB_VENDOR_ID_GYRATION 0x0c16 282#define USB_VENDOR_ID_GYRATION 0x0c16
283#define USB_DEVICE_ID_GYRATION_REMOTE 0x0002 283#define USB_DEVICE_ID_GYRATION_REMOTE 0x0002
284#define USB_DEVICE_ID_GYRATION_REMOTE_2 0x0003 284#define USB_DEVICE_ID_GYRATION_REMOTE_2 0x0003
285#define USB_DEVICE_ID_GYRATION_REMOTE_3 0x0008
285 286
286#define USB_VENDOR_ID_HAPP 0x078b 287#define USB_VENDOR_ID_HAPP 0x078b
287#define USB_DEVICE_ID_UGCI_DRIVING 0x0010 288#define USB_DEVICE_ID_UGCI_DRIVING 0x0010
diff --git a/drivers/hid/hid-roccat-kone.c b/drivers/hid/hid-roccat-kone.c
index 66e694054ba..17f2dc04f88 100644
--- a/drivers/hid/hid-roccat-kone.c
+++ b/drivers/hid/hid-roccat-kone.c
@@ -37,6 +37,7 @@
37#include <linux/module.h> 37#include <linux/module.h>
38#include <linux/slab.h> 38#include <linux/slab.h>
39#include "hid-ids.h" 39#include "hid-ids.h"
40#include "hid-roccat.h"
40#include "hid-roccat-kone.h" 41#include "hid-roccat-kone.h"
41 42
42static void kone_set_settings_checksum(struct kone_settings *settings) 43static void kone_set_settings_checksum(struct kone_settings *settings)
@@ -263,7 +264,7 @@ static int kone_get_firmware_version(struct usb_device *usb_dev, int *result)
263 return 0; 264 return 0;
264} 265}
265 266
266static ssize_t kone_sysfs_read_settings(struct kobject *kobj, 267static ssize_t kone_sysfs_read_settings(struct file *fp, struct kobject *kobj,
267 struct bin_attribute *attr, char *buf, 268 struct bin_attribute *attr, char *buf,
268 loff_t off, size_t count) { 269 loff_t off, size_t count) {
269 struct device *dev = container_of(kobj, struct device, kobj); 270 struct device *dev = container_of(kobj, struct device, kobj);
@@ -287,7 +288,7 @@ static ssize_t kone_sysfs_read_settings(struct kobject *kobj,
287 * This function keeps values in kone_device up to date and assumes that in 288 * This function keeps values in kone_device up to date and assumes that in
288 * case of error the old data is still valid 289 * case of error the old data is still valid
289 */ 290 */
290static ssize_t kone_sysfs_write_settings(struct kobject *kobj, 291static ssize_t kone_sysfs_write_settings(struct file *fp, struct kobject *kobj,
291 struct bin_attribute *attr, char *buf, 292 struct bin_attribute *attr, char *buf,
292 loff_t off, size_t count) { 293 loff_t off, size_t count) {
293 struct device *dev = container_of(kobj, struct device, kobj); 294 struct device *dev = container_of(kobj, struct device, kobj);
@@ -342,31 +343,31 @@ static ssize_t kone_sysfs_read_profilex(struct kobject *kobj,
342 return count; 343 return count;
343} 344}
344 345
345static ssize_t kone_sysfs_read_profile1(struct kobject *kobj, 346static ssize_t kone_sysfs_read_profile1(struct file *fp, struct kobject *kobj,
346 struct bin_attribute *attr, char *buf, 347 struct bin_attribute *attr, char *buf,
347 loff_t off, size_t count) { 348 loff_t off, size_t count) {
348 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 1); 349 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 1);
349} 350}
350 351
351static ssize_t kone_sysfs_read_profile2(struct kobject *kobj, 352static ssize_t kone_sysfs_read_profile2(struct file *fp, struct kobject *kobj,
352 struct bin_attribute *attr, char *buf, 353 struct bin_attribute *attr, char *buf,
353 loff_t off, size_t count) { 354 loff_t off, size_t count) {
354 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 2); 355 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 2);
355} 356}
356 357
357static ssize_t kone_sysfs_read_profile3(struct kobject *kobj, 358static ssize_t kone_sysfs_read_profile3(struct file *fp, struct kobject *kobj,
358 struct bin_attribute *attr, char *buf, 359 struct bin_attribute *attr, char *buf,
359 loff_t off, size_t count) { 360 loff_t off, size_t count) {
360 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 3); 361 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 3);
361} 362}
362 363
363static ssize_t kone_sysfs_read_profile4(struct kobject *kobj, 364static ssize_t kone_sysfs_read_profile4(struct file *fp, struct kobject *kobj,
364 struct bin_attribute *attr, char *buf, 365 struct bin_attribute *attr, char *buf,
365 loff_t off, size_t count) { 366 loff_t off, size_t count) {
366 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 4); 367 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 4);
367} 368}
368 369
369static ssize_t kone_sysfs_read_profile5(struct kobject *kobj, 370static ssize_t kone_sysfs_read_profile5(struct file *fp, struct kobject *kobj,
370 struct bin_attribute *attr, char *buf, 371 struct bin_attribute *attr, char *buf,
371 loff_t off, size_t count) { 372 loff_t off, size_t count) {
372 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 5); 373 return kone_sysfs_read_profilex(kobj, attr, buf, off, count, 5);
@@ -404,31 +405,31 @@ static ssize_t kone_sysfs_write_profilex(struct kobject *kobj,
404 return sizeof(struct kone_profile); 405 return sizeof(struct kone_profile);
405} 406}
406 407
407static ssize_t kone_sysfs_write_profile1(struct kobject *kobj, 408static ssize_t kone_sysfs_write_profile1(struct file *fp, struct kobject *kobj,
408 struct bin_attribute *attr, char *buf, 409 struct bin_attribute *attr, char *buf,
409 loff_t off, size_t count) { 410 loff_t off, size_t count) {
410 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 1); 411 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 1);
411} 412}
412 413
413static ssize_t kone_sysfs_write_profile2(struct kobject *kobj, 414static ssize_t kone_sysfs_write_profile2(struct file *fp, struct kobject *kobj,
414 struct bin_attribute *attr, char *buf, 415 struct bin_attribute *attr, char *buf,
415 loff_t off, size_t count) { 416 loff_t off, size_t count) {
416 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 2); 417 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 2);
417} 418}
418 419
419static ssize_t kone_sysfs_write_profile3(struct kobject *kobj, 420static ssize_t kone_sysfs_write_profile3(struct file *fp, struct kobject *kobj,
420 struct bin_attribute *attr, char *buf, 421 struct bin_attribute *attr, char *buf,
421 loff_t off, size_t count) { 422 loff_t off, size_t count) {
422 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 3); 423 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 3);
423} 424}
424 425
425static ssize_t kone_sysfs_write_profile4(struct kobject *kobj, 426static ssize_t kone_sysfs_write_profile4(struct file *fp, struct kobject *kobj,
426 struct bin_attribute *attr, char *buf, 427 struct bin_attribute *attr, char *buf,
427 loff_t off, size_t count) { 428 loff_t off, size_t count) {
428 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 4); 429 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 4);
429} 430}
430 431
431static ssize_t kone_sysfs_write_profile5(struct kobject *kobj, 432static ssize_t kone_sysfs_write_profile5(struct file *fp, struct kobject *kobj,
432 struct bin_attribute *attr, char *buf, 433 struct bin_attribute *attr, char *buf,
433 loff_t off, size_t count) { 434 loff_t off, size_t count) {
434 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 5); 435 return kone_sysfs_write_profilex(kobj, attr, buf, off, count, 5);
@@ -849,6 +850,16 @@ static int kone_init_specials(struct hid_device *hdev)
849 "couldn't init struct kone_device\n"); 850 "couldn't init struct kone_device\n");
850 goto exit_free; 851 goto exit_free;
851 } 852 }
853
854 retval = roccat_connect(hdev);
855 if (retval < 0) {
856 dev_err(&hdev->dev, "couldn't init char dev\n");
857 /* be tolerant about not getting chrdev */
858 } else {
859 kone->roccat_claimed = 1;
860 kone->chrdev_minor = retval;
861 }
862
852 retval = kone_create_sysfs_attributes(intf); 863 retval = kone_create_sysfs_attributes(intf);
853 if (retval) { 864 if (retval) {
854 dev_err(&hdev->dev, "cannot create sysfs files\n"); 865 dev_err(&hdev->dev, "cannot create sysfs files\n");
@@ -868,10 +879,14 @@ exit_free:
868static void kone_remove_specials(struct hid_device *hdev) 879static void kone_remove_specials(struct hid_device *hdev)
869{ 880{
870 struct usb_interface *intf = to_usb_interface(hdev->dev.parent); 881 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
882 struct kone_device *kone;
871 883
872 if (intf->cur_altsetting->desc.bInterfaceProtocol 884 if (intf->cur_altsetting->desc.bInterfaceProtocol
873 == USB_INTERFACE_PROTOCOL_MOUSE) { 885 == USB_INTERFACE_PROTOCOL_MOUSE) {
874 kone_remove_sysfs_attributes(intf); 886 kone_remove_sysfs_attributes(intf);
887 kone = hid_get_drvdata(hdev);
888 if (kone->roccat_claimed)
889 roccat_disconnect(kone->chrdev_minor);
875 kfree(hid_get_drvdata(hdev)); 890 kfree(hid_get_drvdata(hdev));
876 } 891 }
877} 892}
@@ -930,6 +945,37 @@ static void kone_keep_values_up_to_date(struct kone_device *kone,
930 } 945 }
931} 946}
932 947
948static void kone_report_to_chrdev(struct kone_device const *kone,
949 struct kone_mouse_event const *event)
950{
951 struct kone_roccat_report roccat_report;
952
953 switch (event->event) {
954 case kone_mouse_event_switch_profile:
955 case kone_mouse_event_switch_dpi:
956 case kone_mouse_event_osd_profile:
957 case kone_mouse_event_osd_dpi:
958 roccat_report.event = event->event;
959 roccat_report.value = event->value;
960 roccat_report.key = 0;
961 roccat_report_event(kone->chrdev_minor,
962 (uint8_t *)&roccat_report,
963 sizeof(struct kone_roccat_report));
964 break;
965 case kone_mouse_event_call_overlong_macro:
966 if (event->value == kone_keystroke_action_press) {
967 roccat_report.event = kone_mouse_event_call_overlong_macro;
968 roccat_report.value = kone->actual_profile;
969 roccat_report.key = event->macro_key;
970 roccat_report_event(kone->chrdev_minor,
971 (uint8_t *)&roccat_report,
972 sizeof(struct kone_roccat_report));
973 }
974 break;
975 }
976
977}
978
933/* 979/*
934 * Is called for keyboard- and mousepart. 980 * Is called for keyboard- and mousepart.
935 * Only mousepart gets informations about special events in its extended event 981 * Only mousepart gets informations about special events in its extended event
@@ -958,6 +1004,9 @@ static int kone_raw_event(struct hid_device *hdev, struct hid_report *report,
958 1004
959 kone_keep_values_up_to_date(kone, event); 1005 kone_keep_values_up_to_date(kone, event);
960 1006
1007 if (kone->roccat_claimed)
1008 kone_report_to_chrdev(kone, event);
1009
961 return 0; /* always do further processing */ 1010 return 0; /* always do further processing */
962} 1011}
963 1012
diff --git a/drivers/hid/hid-roccat-kone.h b/drivers/hid/hid-roccat-kone.h
index b413b10a7f8..003e6f81c19 100644
--- a/drivers/hid/hid-roccat-kone.h
+++ b/drivers/hid/hid-roccat-kone.h
@@ -189,6 +189,12 @@ enum kone_commands {
189 kone_command_firmware = 0xe5a 189 kone_command_firmware = 0xe5a
190}; 190};
191 191
192struct kone_roccat_report {
193 uint8_t event;
194 uint8_t value; /* holds dpi or profile value */
195 uint8_t key; /* macro key on overlong macro execution */
196};
197
192#pragma pack(pop) 198#pragma pack(pop)
193 199
194struct kone_device { 200struct kone_device {
@@ -219,6 +225,9 @@ struct kone_device {
219 * so it's read only once 225 * so it's read only once
220 */ 226 */
221 int firmware_version; 227 int firmware_version;
228
229 int roccat_claimed;
230 int chrdev_minor;
222}; 231};
223 232
224#endif 233#endif
diff --git a/drivers/hid/hid-roccat.c b/drivers/hid/hid-roccat.c
new file mode 100644
index 00000000000..e05d48edb66
--- /dev/null
+++ b/drivers/hid/hid-roccat.c
@@ -0,0 +1,428 @@
1/*
2 * Roccat 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 * Module roccat is a char device used to report special events of roccat
16 * hardware to userland. These events include requests for on-screen-display of
17 * profile or dpi settings or requests for execution of macro sequences that are
18 * not stored in device. The information in these events depends on hid device
19 * implementation and contains data that is not available in a single hid event
20 * or else hidraw could have been used.
21 * It is inspired by hidraw, but uses only one circular buffer for all readers.
22 */
23
24#include <linux/cdev.h>
25#include <linux/poll.h>
26#include <linux/sched.h>
27
28#include "hid-roccat.h"
29
30#define ROCCAT_FIRST_MINOR 0
31#define ROCCAT_MAX_DEVICES 8
32
33/* should be a power of 2 for performance reason */
34#define ROCCAT_CBUF_SIZE 16
35
36struct roccat_report {
37 uint8_t *value;
38 int len;
39};
40
41struct roccat_device {
42 unsigned int minor;
43 int open;
44 int exist;
45 wait_queue_head_t wait;
46 struct device *dev;
47 struct hid_device *hid;
48 struct list_head readers;
49 /* protects modifications of readers list */
50 struct mutex readers_lock;
51
52 /*
53 * circular_buffer has one writer and multiple readers with their own
54 * read pointers
55 */
56 struct roccat_report cbuf[ROCCAT_CBUF_SIZE];
57 int cbuf_end;
58 struct mutex cbuf_lock;
59};
60
61struct roccat_reader {
62 struct list_head node;
63 struct roccat_device *device;
64 int cbuf_start;
65};
66
67static int roccat_major;
68static struct class *roccat_class;
69static struct cdev roccat_cdev;
70
71static struct roccat_device *devices[ROCCAT_MAX_DEVICES];
72/* protects modifications of devices array */
73static DEFINE_MUTEX(devices_lock);
74
75static ssize_t roccat_read(struct file *file, char __user *buffer,
76 size_t count, loff_t *ppos)
77{
78 struct roccat_reader *reader = file->private_data;
79 struct roccat_device *device = reader->device;
80 struct roccat_report *report;
81 ssize_t retval = 0, len;
82 DECLARE_WAITQUEUE(wait, current);
83
84 mutex_lock(&device->cbuf_lock);
85
86 /* no data? */
87 if (reader->cbuf_start == device->cbuf_end) {
88 add_wait_queue(&device->wait, &wait);
89 set_current_state(TASK_INTERRUPTIBLE);
90
91 /* wait for data */
92 while (reader->cbuf_start == device->cbuf_end) {
93 if (file->f_flags & O_NONBLOCK) {
94 retval = -EAGAIN;
95 break;
96 }
97 if (signal_pending(current)) {
98 retval = -ERESTARTSYS;
99 break;
100 }
101 if (!device->exist) {
102 retval = -EIO;
103 break;
104 }
105
106 mutex_unlock(&device->cbuf_lock);
107 schedule();
108 mutex_lock(&device->cbuf_lock);
109 set_current_state(TASK_INTERRUPTIBLE);
110 }
111
112 set_current_state(TASK_RUNNING);
113 remove_wait_queue(&device->wait, &wait);
114 }
115
116 /* here we either have data or a reason to return if retval is set */
117 if (retval)
118 goto exit_unlock;
119
120 report = &device->cbuf[reader->cbuf_start];
121 /*
122 * If report is larger than requested amount of data, rest of report
123 * is lost!
124 */
125 len = report->len > count ? count : report->len;
126
127 if (copy_to_user(buffer, report->value, len)) {
128 retval = -EFAULT;
129 goto exit_unlock;
130 }
131 retval += len;
132 reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
133
134exit_unlock:
135 mutex_unlock(&device->cbuf_lock);
136 return retval;
137}
138
139static unsigned int roccat_poll(struct file *file, poll_table *wait)
140{
141 struct roccat_reader *reader = file->private_data;
142 poll_wait(file, &reader->device->wait, wait);
143 if (reader->cbuf_start != reader->device->cbuf_end)
144 return POLLIN | POLLRDNORM;
145 if (!reader->device->exist)
146 return POLLERR | POLLHUP;
147 return 0;
148}
149
150static int roccat_open(struct inode *inode, struct file *file)
151{
152 unsigned int minor = iminor(inode);
153 struct roccat_reader *reader;
154 struct roccat_device *device;
155 int error = 0;
156
157 reader = kzalloc(sizeof(struct roccat_reader), GFP_KERNEL);
158 if (!reader)
159 return -ENOMEM;
160
161 mutex_lock(&devices_lock);
162
163 device = devices[minor];
164
165 mutex_lock(&device->readers_lock);
166
167 if (!device) {
168 printk(KERN_EMERG "roccat device with minor %d doesn't exist\n",
169 minor);
170 error = -ENODEV;
171 goto exit_unlock;
172 }
173
174 if (!device->open++) {
175 /* power on device on adding first reader */
176 if (device->hid->ll_driver->power) {
177 error = device->hid->ll_driver->power(device->hid,
178 PM_HINT_FULLON);
179 if (error < 0) {
180 --device->open;
181 goto exit_unlock;
182 }
183 }
184 error = device->hid->ll_driver->open(device->hid);
185 if (error < 0) {
186 if (device->hid->ll_driver->power)
187 device->hid->ll_driver->power(device->hid,
188 PM_HINT_NORMAL);
189 --device->open;
190 goto exit_unlock;
191 }
192 }
193
194 reader->device = device;
195 /* new reader doesn't get old events */
196 reader->cbuf_start = device->cbuf_end;
197
198 list_add_tail(&reader->node, &device->readers);
199 file->private_data = reader;
200
201exit_unlock:
202 mutex_unlock(&device->readers_lock);
203 mutex_unlock(&devices_lock);
204 return error;
205}
206
207static int roccat_release(struct inode *inode, struct file *file)
208{
209 unsigned int minor = iminor(inode);
210 struct roccat_reader *reader = file->private_data;
211 struct roccat_device *device;
212
213 mutex_lock(&devices_lock);
214
215 device = devices[minor];
216 if (!device) {
217 mutex_unlock(&devices_lock);
218 printk(KERN_EMERG "roccat device with minor %d doesn't exist\n",
219 minor);
220 return -ENODEV;
221 }
222
223 mutex_lock(&device->readers_lock);
224 list_del(&reader->node);
225 mutex_unlock(&device->readers_lock);
226 kfree(reader);
227
228 if (!--device->open) {
229 /* removing last reader */
230 if (device->exist) {
231 if (device->hid->ll_driver->power)
232 device->hid->ll_driver->power(device->hid,
233 PM_HINT_NORMAL);
234 device->hid->ll_driver->close(device->hid);
235 } else {
236 kfree(device);
237 }
238 }
239
240 mutex_unlock(&devices_lock);
241
242 return 0;
243}
244
245/*
246 * roccat_report_event() - output data to readers
247 * @minor: minor device number returned by roccat_connect()
248 * @data: pointer to data
249 * @len: size of data
250 *
251 * Return value is zero on success, a negative error code on failure.
252 *
253 * This is called from interrupt handler.
254 */
255int roccat_report_event(int minor, u8 const *data, int len)
256{
257 struct roccat_device *device;
258 struct roccat_reader *reader;
259 struct roccat_report *report;
260 uint8_t *new_value;
261
262 new_value = kmemdup(data, len, GFP_ATOMIC);
263 if (!new_value)
264 return -ENOMEM;
265
266 device = devices[minor];
267
268 report = &device->cbuf[device->cbuf_end];
269
270 /* passing NULL is safe */
271 kfree(report->value);
272
273 report->value = new_value;
274 report->len = len;
275 device->cbuf_end = (device->cbuf_end + 1) % ROCCAT_CBUF_SIZE;
276
277 list_for_each_entry(reader, &device->readers, node) {
278 /*
279 * As we already inserted one element, the buffer can't be
280 * empty. If start and end are equal, buffer is full and we
281 * increase start, so that slow reader misses one event, but
282 * gets the newer ones in the right order.
283 */
284 if (reader->cbuf_start == device->cbuf_end)
285 reader->cbuf_start = (reader->cbuf_start + 1) % ROCCAT_CBUF_SIZE;
286 }
287
288 wake_up_interruptible(&device->wait);
289 return 0;
290}
291EXPORT_SYMBOL_GPL(roccat_report_event);
292
293/*
294 * roccat_connect() - create a char device for special event output
295 * @hid: the hid device the char device should be connected to.
296 *
297 * Return value is minor device number in Range [0, ROCCAT_MAX_DEVICES] on
298 * success, a negative error code on failure.
299 */
300int roccat_connect(struct hid_device *hid)
301{
302 unsigned int minor;
303 struct roccat_device *device;
304 int temp;
305
306 device = kzalloc(sizeof(struct roccat_device), GFP_KERNEL);
307 if (!device)
308 return -ENOMEM;
309
310 mutex_lock(&devices_lock);
311
312 for (minor = 0; minor < ROCCAT_MAX_DEVICES; ++minor) {
313 if (devices[minor])
314 continue;
315 break;
316 }
317
318 if (minor < ROCCAT_MAX_DEVICES) {
319 devices[minor] = device;
320 } else {
321 mutex_unlock(&devices_lock);
322 kfree(device);
323 return -EINVAL;
324 }
325
326 device->dev = device_create(roccat_class, &hid->dev,
327 MKDEV(roccat_major, minor), NULL,
328 "%s%s%d", "roccat", hid->driver->name, minor);
329
330 if (IS_ERR(device->dev)) {
331 devices[minor] = NULL;
332 mutex_unlock(&devices_lock);
333 temp = PTR_ERR(device->dev);
334 kfree(device);
335 return temp;
336 }
337
338 mutex_unlock(&devices_lock);
339
340 init_waitqueue_head(&device->wait);
341 INIT_LIST_HEAD(&device->readers);
342 mutex_init(&device->readers_lock);
343 mutex_init(&device->cbuf_lock);
344 device->minor = minor;
345 device->hid = hid;
346 device->exist = 1;
347 device->cbuf_end = 0;
348
349 return minor;
350}
351EXPORT_SYMBOL_GPL(roccat_connect);
352
353/* roccat_disconnect() - remove char device from hid device
354 * @minor: the minor device number returned by roccat_connect()
355 */
356void roccat_disconnect(int minor)
357{
358 struct roccat_device *device;
359
360 mutex_lock(&devices_lock);
361 device = devices[minor];
362 devices[minor] = NULL;
363 mutex_unlock(&devices_lock);
364
365 device->exist = 0; /* TODO exist maybe not needed */
366
367 device_destroy(roccat_class, MKDEV(roccat_major, minor));
368
369 if (device->open) {
370 device->hid->ll_driver->close(device->hid);
371 wake_up_interruptible(&device->wait);
372 } else {
373 kfree(device);
374 }
375}
376EXPORT_SYMBOL_GPL(roccat_disconnect);
377
378static const struct file_operations roccat_ops = {
379 .owner = THIS_MODULE,
380 .read = roccat_read,
381 .poll = roccat_poll,
382 .open = roccat_open,
383 .release = roccat_release,
384};
385
386static int __init roccat_init(void)
387{
388 int retval;
389 dev_t dev_id;
390
391 retval = alloc_chrdev_region(&dev_id, ROCCAT_FIRST_MINOR,
392 ROCCAT_MAX_DEVICES, "roccat");
393
394 roccat_major = MAJOR(dev_id);
395
396 if (retval < 0) {
397 printk(KERN_WARNING "roccat: can't get major number\n");
398 return retval;
399 }
400
401 roccat_class = class_create(THIS_MODULE, "roccat");
402 if (IS_ERR(roccat_class)) {
403 retval = PTR_ERR(roccat_class);
404 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
405 return retval;
406 }
407
408 cdev_init(&roccat_cdev, &roccat_ops);
409 cdev_add(&roccat_cdev, dev_id, ROCCAT_MAX_DEVICES);
410
411 return 0;
412}
413
414static void __exit roccat_exit(void)
415{
416 dev_t dev_id = MKDEV(roccat_major, 0);
417
418 cdev_del(&roccat_cdev);
419 class_destroy(roccat_class);
420 unregister_chrdev_region(dev_id, ROCCAT_MAX_DEVICES);
421}
422
423module_init(roccat_init);
424module_exit(roccat_exit);
425
426MODULE_AUTHOR("Stefan Achatz");
427MODULE_DESCRIPTION("USB Roccat char device");
428MODULE_LICENSE("GPL v2");
diff --git a/drivers/hid/hid-roccat.h b/drivers/hid/hid-roccat.h
new file mode 100644
index 00000000000..d8aae0c1fa7
--- /dev/null
+++ b/drivers/hid/hid-roccat.h
@@ -0,0 +1,31 @@
1#ifndef __HID_ROCCAT_H
2#define __HID_ROCCAT_H
3
4/*
5 * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
6 */
7
8/*
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the License, or (at your option)
12 * any later version.
13 */
14
15#include <linux/hid.h>
16#include <linux/types.h>
17
18#if defined(CONFIG_HID_ROCCAT) || defined (CONFIG_HID_ROCCAT_MODULE)
19int roccat_connect(struct hid_device *hid);
20void roccat_disconnect(int minor);
21int roccat_report_event(int minor, u8 const *data, int len);
22#else
23static inline int roccat_connect(struct hid_device *hid) { return -1; }
24static inline void roccat_disconnect(int minor) {}
25static inline int roccat_report_event(int minor, u8 const *data, int len)
26{
27 return 0;
28}
29#endif
30
31#endif