Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gerg/m68knommu
[linux-2.6.git] / drivers / hid / hid-roccat-konepure.c
blobc79d0b06c143d8aa1a0bd6dfacdad5a9c56b16e0
1 /*
2 * Roccat KonePure driver for Linux
4 * Copyright (c) 2012 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 KonePure is a smaller version of KoneXTD with less buttons and lights.
18 #include <linux/device.h>
19 #include <linux/input.h>
20 #include <linux/hid.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/hid-roccat.h>
24 #include "hid-ids.h"
25 #include "hid-roccat-common.h"
26 #include "hid-roccat-konepure.h"
28 static struct class *konepure_class;
30 static ssize_t konepure_sysfs_read(struct file *fp, struct kobject *kobj,
31 char *buf, loff_t off, size_t count,
32 size_t real_size, uint command)
34 struct device *dev =
35 container_of(kobj, struct device, kobj)->parent->parent;
36 struct konepure_device *konepure = hid_get_drvdata(dev_get_drvdata(dev));
37 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
38 int retval;
40 if (off >= real_size)
41 return 0;
43 if (off != 0 || count != real_size)
44 return -EINVAL;
46 mutex_lock(&konepure->konepure_lock);
47 retval = roccat_common2_receive(usb_dev, command, buf, real_size);
48 mutex_unlock(&konepure->konepure_lock);
50 return retval ? retval : real_size;
53 static ssize_t konepure_sysfs_write(struct file *fp, struct kobject *kobj,
54 void const *buf, loff_t off, size_t count,
55 size_t real_size, uint command)
57 struct device *dev =
58 container_of(kobj, struct device, kobj)->parent->parent;
59 struct konepure_device *konepure = hid_get_drvdata(dev_get_drvdata(dev));
60 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
61 int retval;
63 if (off != 0 || count != real_size)
64 return -EINVAL;
66 mutex_lock(&konepure->konepure_lock);
67 retval = roccat_common2_send_with_status(usb_dev, command,
68 (void *)buf, real_size);
69 mutex_unlock(&konepure->konepure_lock);
71 return retval ? retval : real_size;
74 #define KONEPURE_SYSFS_W(thingy, THINGY) \
75 static ssize_t konepure_sysfs_write_ ## thingy(struct file *fp, \
76 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
77 loff_t off, size_t count) \
78 { \
79 return konepure_sysfs_write(fp, kobj, buf, off, count, \
80 KONEPURE_SIZE_ ## THINGY, KONEPURE_COMMAND_ ## THINGY); \
83 #define KONEPURE_SYSFS_R(thingy, THINGY) \
84 static ssize_t konepure_sysfs_read_ ## thingy(struct file *fp, \
85 struct kobject *kobj, struct bin_attribute *attr, char *buf, \
86 loff_t off, size_t count) \
87 { \
88 return konepure_sysfs_read(fp, kobj, buf, off, count, \
89 KONEPURE_SIZE_ ## THINGY, KONEPURE_COMMAND_ ## THINGY); \
92 #define KONEPURE_SYSFS_RW(thingy, THINGY) \
93 KONEPURE_SYSFS_W(thingy, THINGY) \
94 KONEPURE_SYSFS_R(thingy, THINGY)
96 #define KONEPURE_BIN_ATTRIBUTE_RW(thingy, THINGY) \
97 { \
98 .attr = { .name = #thingy, .mode = 0660 }, \
99 .size = KONEPURE_SIZE_ ## THINGY, \
100 .read = konepure_sysfs_read_ ## thingy, \
101 .write = konepure_sysfs_write_ ## thingy \
104 #define KONEPURE_BIN_ATTRIBUTE_R(thingy, THINGY) \
106 .attr = { .name = #thingy, .mode = 0440 }, \
107 .size = KONEPURE_SIZE_ ## THINGY, \
108 .read = konepure_sysfs_read_ ## thingy, \
111 #define KONEPURE_BIN_ATTRIBUTE_W(thingy, THINGY) \
113 .attr = { .name = #thingy, .mode = 0220 }, \
114 .size = KONEPURE_SIZE_ ## THINGY, \
115 .write = konepure_sysfs_write_ ## thingy \
118 KONEPURE_SYSFS_RW(actual_profile, ACTUAL_PROFILE)
119 KONEPURE_SYSFS_W(control, CONTROL)
120 KONEPURE_SYSFS_RW(info, INFO)
121 KONEPURE_SYSFS_W(talk, TALK)
122 KONEPURE_SYSFS_W(macro, MACRO)
123 KONEPURE_SYSFS_RW(sensor, SENSOR)
124 KONEPURE_SYSFS_RW(tcu, TCU)
125 KONEPURE_SYSFS_R(tcu_image, TCU_IMAGE)
126 KONEPURE_SYSFS_RW(profile_settings, PROFILE_SETTINGS)
127 KONEPURE_SYSFS_RW(profile_buttons, PROFILE_BUTTONS)
129 static struct bin_attribute konepure_bin_attributes[] = {
130 KONEPURE_BIN_ATTRIBUTE_RW(actual_profile, ACTUAL_PROFILE),
131 KONEPURE_BIN_ATTRIBUTE_W(control, CONTROL),
132 KONEPURE_BIN_ATTRIBUTE_RW(info, INFO),
133 KONEPURE_BIN_ATTRIBUTE_W(talk, TALK),
134 KONEPURE_BIN_ATTRIBUTE_W(macro, MACRO),
135 KONEPURE_BIN_ATTRIBUTE_RW(sensor, SENSOR),
136 KONEPURE_BIN_ATTRIBUTE_RW(tcu, TCU),
137 KONEPURE_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE),
138 KONEPURE_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS),
139 KONEPURE_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS),
140 __ATTR_NULL
143 static int konepure_init_konepure_device_struct(struct usb_device *usb_dev,
144 struct konepure_device *konepure)
146 mutex_init(&konepure->konepure_lock);
148 return 0;
151 static int konepure_init_specials(struct hid_device *hdev)
153 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
154 struct usb_device *usb_dev = interface_to_usbdev(intf);
155 struct konepure_device *konepure;
156 int retval;
158 if (intf->cur_altsetting->desc.bInterfaceProtocol
159 != USB_INTERFACE_PROTOCOL_MOUSE) {
160 hid_set_drvdata(hdev, NULL);
161 return 0;
164 konepure = kzalloc(sizeof(*konepure), GFP_KERNEL);
165 if (!konepure) {
166 hid_err(hdev, "can't alloc device descriptor\n");
167 return -ENOMEM;
169 hid_set_drvdata(hdev, konepure);
171 retval = konepure_init_konepure_device_struct(usb_dev, konepure);
172 if (retval) {
173 hid_err(hdev, "couldn't init struct konepure_device\n");
174 goto exit_free;
177 retval = roccat_connect(konepure_class, hdev,
178 sizeof(struct konepure_mouse_report_button));
179 if (retval < 0) {
180 hid_err(hdev, "couldn't init char dev\n");
181 } else {
182 konepure->chrdev_minor = retval;
183 konepure->roccat_claimed = 1;
186 return 0;
187 exit_free:
188 kfree(konepure);
189 return retval;
192 static void konepure_remove_specials(struct hid_device *hdev)
194 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
195 struct konepure_device *konepure;
197 if (intf->cur_altsetting->desc.bInterfaceProtocol
198 != USB_INTERFACE_PROTOCOL_MOUSE)
199 return;
201 konepure = hid_get_drvdata(hdev);
202 if (konepure->roccat_claimed)
203 roccat_disconnect(konepure->chrdev_minor);
204 kfree(konepure);
207 static int konepure_probe(struct hid_device *hdev,
208 const struct hid_device_id *id)
210 int retval;
212 retval = hid_parse(hdev);
213 if (retval) {
214 hid_err(hdev, "parse failed\n");
215 goto exit;
218 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
219 if (retval) {
220 hid_err(hdev, "hw start failed\n");
221 goto exit;
224 retval = konepure_init_specials(hdev);
225 if (retval) {
226 hid_err(hdev, "couldn't install mouse\n");
227 goto exit_stop;
230 return 0;
232 exit_stop:
233 hid_hw_stop(hdev);
234 exit:
235 return retval;
238 static void konepure_remove(struct hid_device *hdev)
240 konepure_remove_specials(hdev);
241 hid_hw_stop(hdev);
244 static int konepure_raw_event(struct hid_device *hdev,
245 struct hid_report *report, u8 *data, int size)
247 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
248 struct konepure_device *konepure = hid_get_drvdata(hdev);
250 if (intf->cur_altsetting->desc.bInterfaceProtocol
251 != USB_INTERFACE_PROTOCOL_MOUSE)
252 return 0;
254 if (data[0] != KONEPURE_MOUSE_REPORT_NUMBER_BUTTON)
255 return 0;
257 if (konepure != NULL && konepure->roccat_claimed)
258 roccat_report_event(konepure->chrdev_minor, data);
260 return 0;
263 static const struct hid_device_id konepure_devices[] = {
264 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPURE) },
268 MODULE_DEVICE_TABLE(hid, konepure_devices);
270 static struct hid_driver konepure_driver = {
271 .name = "konepure",
272 .id_table = konepure_devices,
273 .probe = konepure_probe,
274 .remove = konepure_remove,
275 .raw_event = konepure_raw_event
278 static int __init konepure_init(void)
280 int retval;
282 konepure_class = class_create(THIS_MODULE, "konepure");
283 if (IS_ERR(konepure_class))
284 return PTR_ERR(konepure_class);
285 konepure_class->dev_bin_attrs = konepure_bin_attributes;
287 retval = hid_register_driver(&konepure_driver);
288 if (retval)
289 class_destroy(konepure_class);
290 return retval;
293 static void __exit konepure_exit(void)
295 hid_unregister_driver(&konepure_driver);
296 class_destroy(konepure_class);
299 module_init(konepure_init);
300 module_exit(konepure_exit);
302 MODULE_AUTHOR("Stefan Achatz");
303 MODULE_DESCRIPTION("USB Roccat KonePure driver");
304 MODULE_LICENSE("GPL v2");