blob: 8023751d525766ae16b32f8a8943f8c5f9b0796e [file] [log] [blame]
Stefan Achatzd41c2a72011-11-24 17:46:24 +01001/*
2 * Roccat Isku driver for Linux
3 *
4 * Copyright (c) 2011 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 Isku is a gamer keyboard with macro keys that can be configured in
16 * 5 profiles.
17 */
18
19#include <linux/device.h>
20#include <linux/input.h>
21#include <linux/hid.h>
22#include <linux/module.h>
23#include <linux/slab.h>
24#include <linux/hid-roccat.h>
25#include "hid-ids.h"
26#include "hid-roccat-common.h"
27#include "hid-roccat-isku.h"
28
29static struct class *isku_class;
30
31static void isku_profile_activated(struct isku_device *isku, uint new_profile)
32{
33 isku->actual_profile = new_profile;
34}
35
36static int isku_receive(struct usb_device *usb_dev, uint command,
37 void *buf, uint size)
38{
Stefan Achatz7392d732012-05-20 22:45:04 +020039 return roccat_common2_receive(usb_dev, command, buf, size);
Stefan Achatzd41c2a72011-11-24 17:46:24 +010040}
41
Stefan Achatzd41c2a72011-11-24 17:46:24 +010042static int isku_get_actual_profile(struct usb_device *usb_dev)
43{
44 struct isku_actual_profile buf;
45 int retval;
46
47 retval = isku_receive(usb_dev, ISKU_COMMAND_ACTUAL_PROFILE,
48 &buf, sizeof(struct isku_actual_profile));
49 return retval ? retval : buf.actual_profile;
50}
51
52static int isku_set_actual_profile(struct usb_device *usb_dev, int new_profile)
53{
54 struct isku_actual_profile buf;
55
56 buf.command = ISKU_COMMAND_ACTUAL_PROFILE;
57 buf.size = sizeof(struct isku_actual_profile);
58 buf.actual_profile = new_profile;
Stefan Achatz7392d732012-05-20 22:45:04 +020059 return roccat_common2_send_with_status(usb_dev,
Stefan Achatz4728f2d2012-05-20 22:44:59 +020060 ISKU_COMMAND_ACTUAL_PROFILE, &buf,
Stefan Achatzd41c2a72011-11-24 17:46:24 +010061 sizeof(struct isku_actual_profile));
62}
63
64static ssize_t isku_sysfs_show_actual_profile(struct device *dev,
65 struct device_attribute *attr, char *buf)
66{
67 struct isku_device *isku =
68 hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
69 return snprintf(buf, PAGE_SIZE, "%d\n", isku->actual_profile);
70}
71
72static ssize_t isku_sysfs_set_actual_profile(struct device *dev,
73 struct device_attribute *attr, char const *buf, size_t size)
74{
75 struct isku_device *isku;
76 struct usb_device *usb_dev;
77 unsigned long profile;
78 int retval;
79 struct isku_roccat_report roccat_report;
80
81 dev = dev->parent->parent;
82 isku = hid_get_drvdata(dev_get_drvdata(dev));
83 usb_dev = interface_to_usbdev(to_usb_interface(dev));
84
85 retval = strict_strtoul(buf, 10, &profile);
86 if (retval)
87 return retval;
88
89 if (profile > 4)
90 return -EINVAL;
91
92 mutex_lock(&isku->isku_lock);
93
94 retval = isku_set_actual_profile(usb_dev, profile);
95 if (retval) {
96 mutex_unlock(&isku->isku_lock);
97 return retval;
98 }
99
100 isku_profile_activated(isku, profile);
101
102 roccat_report.event = ISKU_REPORT_BUTTON_EVENT_PROFILE;
103 roccat_report.data1 = profile + 1;
104 roccat_report.data2 = 0;
105 roccat_report.profile = profile + 1;
106 roccat_report_event(isku->chrdev_minor, (uint8_t const *)&roccat_report);
107
108 mutex_unlock(&isku->isku_lock);
109
110 return size;
111}
112
113static struct device_attribute isku_attributes[] = {
114 __ATTR(actual_profile, 0660,
115 isku_sysfs_show_actual_profile,
116 isku_sysfs_set_actual_profile),
117 __ATTR_NULL
118};
119
120static ssize_t isku_sysfs_read(struct file *fp, struct kobject *kobj,
121 char *buf, loff_t off, size_t count,
122 size_t real_size, uint command)
123{
124 struct device *dev =
125 container_of(kobj, struct device, kobj)->parent->parent;
126 struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
127 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
128 int retval;
129
130 if (off >= real_size)
131 return 0;
132
Stefan Achatzce716962013-03-10 12:33:02 +0100133 if (off != 0 || count > real_size)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100134 return -EINVAL;
135
136 mutex_lock(&isku->isku_lock);
Stefan Achatzce716962013-03-10 12:33:02 +0100137 retval = isku_receive(usb_dev, command, buf, count);
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100138 mutex_unlock(&isku->isku_lock);
139
Stefan Achatzce716962013-03-10 12:33:02 +0100140 return retval ? retval : count;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100141}
142
143static ssize_t isku_sysfs_write(struct file *fp, struct kobject *kobj,
144 void const *buf, loff_t off, size_t count,
145 size_t real_size, uint command)
146{
147 struct device *dev =
148 container_of(kobj, struct device, kobj)->parent->parent;
149 struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
150 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
151 int retval;
152
Stefan Achatzce716962013-03-10 12:33:02 +0100153 if (off != 0 || count > real_size)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100154 return -EINVAL;
155
156 mutex_lock(&isku->isku_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200157 retval = roccat_common2_send_with_status(usb_dev, command,
Stefan Achatzce716962013-03-10 12:33:02 +0100158 (void *)buf, count);
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100159 mutex_unlock(&isku->isku_lock);
160
Stefan Achatzce716962013-03-10 12:33:02 +0100161 return retval ? retval : count;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100162}
163
164#define ISKU_SYSFS_W(thingy, THINGY) \
165static ssize_t isku_sysfs_write_ ## thingy(struct file *fp, struct kobject *kobj, \
166 struct bin_attribute *attr, char *buf, \
167 loff_t off, size_t count) \
168{ \
169 return isku_sysfs_write(fp, kobj, buf, off, count, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100170 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100171}
172
173#define ISKU_SYSFS_R(thingy, THINGY) \
174static ssize_t isku_sysfs_read_ ## thingy(struct file *fp, struct kobject *kobj, \
175 struct bin_attribute *attr, char *buf, \
176 loff_t off, size_t count) \
177{ \
178 return isku_sysfs_read(fp, kobj, buf, off, count, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100179 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100180}
181
182#define ISKU_SYSFS_RW(thingy, THINGY) \
183ISKU_SYSFS_R(thingy, THINGY) \
184ISKU_SYSFS_W(thingy, THINGY)
185
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100186#define ISKU_BIN_ATTR_RW(thingy, THINGY) \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100187{ \
188 .attr = { .name = #thingy, .mode = 0660 }, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100189 .size = ISKU_SIZE_ ## THINGY, \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100190 .read = isku_sysfs_read_ ## thingy, \
191 .write = isku_sysfs_write_ ## thingy \
192}
193
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100194#define ISKU_BIN_ATTR_R(thingy, THINGY) \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100195{ \
196 .attr = { .name = #thingy, .mode = 0440 }, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100197 .size = ISKU_SIZE_ ## THINGY, \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100198 .read = isku_sysfs_read_ ## thingy, \
199}
200
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100201#define ISKU_BIN_ATTR_W(thingy, THINGY) \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100202{ \
203 .attr = { .name = #thingy, .mode = 0220 }, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100204 .size = ISKU_SIZE_ ## THINGY, \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100205 .write = isku_sysfs_write_ ## thingy \
206}
207
208ISKU_SYSFS_RW(macro, MACRO)
209ISKU_SYSFS_RW(keys_function, KEYS_FUNCTION)
210ISKU_SYSFS_RW(keys_easyzone, KEYS_EASYZONE)
211ISKU_SYSFS_RW(keys_media, KEYS_MEDIA)
212ISKU_SYSFS_RW(keys_thumbster, KEYS_THUMBSTER)
213ISKU_SYSFS_RW(keys_macro, KEYS_MACRO)
214ISKU_SYSFS_RW(keys_capslock, KEYS_CAPSLOCK)
215ISKU_SYSFS_RW(light, LIGHT)
216ISKU_SYSFS_RW(key_mask, KEY_MASK)
217ISKU_SYSFS_RW(last_set, LAST_SET)
218ISKU_SYSFS_W(talk, TALK)
Stefan Achatzce716962013-03-10 12:33:02 +0100219ISKU_SYSFS_W(talkfx, TALKFX)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100220ISKU_SYSFS_R(info, INFO)
221ISKU_SYSFS_W(control, CONTROL)
Stefan Achatzf1da71e2012-11-11 06:21:06 +0100222ISKU_SYSFS_W(reset, RESET)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100223
224static struct bin_attribute isku_bin_attributes[] = {
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100225 ISKU_BIN_ATTR_RW(macro, MACRO),
226 ISKU_BIN_ATTR_RW(keys_function, KEYS_FUNCTION),
227 ISKU_BIN_ATTR_RW(keys_easyzone, KEYS_EASYZONE),
228 ISKU_BIN_ATTR_RW(keys_media, KEYS_MEDIA),
229 ISKU_BIN_ATTR_RW(keys_thumbster, KEYS_THUMBSTER),
230 ISKU_BIN_ATTR_RW(keys_macro, KEYS_MACRO),
231 ISKU_BIN_ATTR_RW(keys_capslock, KEYS_CAPSLOCK),
232 ISKU_BIN_ATTR_RW(light, LIGHT),
233 ISKU_BIN_ATTR_RW(key_mask, KEY_MASK),
234 ISKU_BIN_ATTR_RW(last_set, LAST_SET),
235 ISKU_BIN_ATTR_W(talk, TALK),
Stefan Achatzce716962013-03-10 12:33:02 +0100236 ISKU_BIN_ATTR_W(talkfx, TALKFX),
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100237 ISKU_BIN_ATTR_R(info, INFO),
238 ISKU_BIN_ATTR_W(control, CONTROL),
Stefan Achatzf1da71e2012-11-11 06:21:06 +0100239 ISKU_BIN_ATTR_W(reset, RESET),
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100240 __ATTR_NULL
241};
242
243static int isku_init_isku_device_struct(struct usb_device *usb_dev,
244 struct isku_device *isku)
245{
246 int retval;
247
248 mutex_init(&isku->isku_lock);
249
250 retval = isku_get_actual_profile(usb_dev);
251 if (retval < 0)
252 return retval;
253 isku_profile_activated(isku, retval);
254
255 return 0;
256}
257
258static int isku_init_specials(struct hid_device *hdev)
259{
260 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
261 struct usb_device *usb_dev = interface_to_usbdev(intf);
262 struct isku_device *isku;
263 int retval;
264
265 if (intf->cur_altsetting->desc.bInterfaceProtocol
266 != ISKU_USB_INTERFACE_PROTOCOL) {
267 hid_set_drvdata(hdev, NULL);
268 return 0;
269 }
270
271 isku = kzalloc(sizeof(*isku), GFP_KERNEL);
272 if (!isku) {
273 hid_err(hdev, "can't alloc device descriptor\n");
274 return -ENOMEM;
275 }
276 hid_set_drvdata(hdev, isku);
277
278 retval = isku_init_isku_device_struct(usb_dev, isku);
279 if (retval) {
280 hid_err(hdev, "couldn't init struct isku_device\n");
281 goto exit_free;
282 }
283
284 retval = roccat_connect(isku_class, hdev,
285 sizeof(struct isku_roccat_report));
286 if (retval < 0) {
287 hid_err(hdev, "couldn't init char dev\n");
288 } else {
289 isku->chrdev_minor = retval;
290 isku->roccat_claimed = 1;
291 }
292
293 return 0;
294exit_free:
295 kfree(isku);
296 return retval;
297}
298
299static void isku_remove_specials(struct hid_device *hdev)
300{
301 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
302 struct isku_device *isku;
303
304 if (intf->cur_altsetting->desc.bInterfaceProtocol
305 != ISKU_USB_INTERFACE_PROTOCOL)
306 return;
307
308 isku = hid_get_drvdata(hdev);
309 if (isku->roccat_claimed)
310 roccat_disconnect(isku->chrdev_minor);
311 kfree(isku);
312}
313
314static int isku_probe(struct hid_device *hdev,
315 const struct hid_device_id *id)
316{
317 int retval;
318
319 retval = hid_parse(hdev);
320 if (retval) {
321 hid_err(hdev, "parse failed\n");
322 goto exit;
323 }
324
325 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
326 if (retval) {
327 hid_err(hdev, "hw start failed\n");
328 goto exit;
329 }
330
331 retval = isku_init_specials(hdev);
332 if (retval) {
333 hid_err(hdev, "couldn't install keyboard\n");
334 goto exit_stop;
335 }
336
337 return 0;
338
339exit_stop:
340 hid_hw_stop(hdev);
341exit:
342 return retval;
343}
344
345static void isku_remove(struct hid_device *hdev)
346{
347 isku_remove_specials(hdev);
348 hid_hw_stop(hdev);
349}
350
351static void isku_keep_values_up_to_date(struct isku_device *isku,
352 u8 const *data)
353{
354 struct isku_report_button const *button_report;
355
356 switch (data[0]) {
357 case ISKU_REPORT_NUMBER_BUTTON:
358 button_report = (struct isku_report_button const *)data;
359 switch (button_report->event) {
360 case ISKU_REPORT_BUTTON_EVENT_PROFILE:
361 isku_profile_activated(isku, button_report->data1 - 1);
362 break;
363 }
364 break;
365 }
366}
367
368static void isku_report_to_chrdev(struct isku_device const *isku,
369 u8 const *data)
370{
371 struct isku_roccat_report roccat_report;
372 struct isku_report_button const *button_report;
373
374 if (data[0] != ISKU_REPORT_NUMBER_BUTTON)
375 return;
376
377 button_report = (struct isku_report_button const *)data;
378
379 roccat_report.event = button_report->event;
380 roccat_report.data1 = button_report->data1;
381 roccat_report.data2 = button_report->data2;
382 roccat_report.profile = isku->actual_profile + 1;
383 roccat_report_event(isku->chrdev_minor,
384 (uint8_t const *)&roccat_report);
385}
386
387static int isku_raw_event(struct hid_device *hdev,
388 struct hid_report *report, u8 *data, int size)
389{
390 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
391 struct isku_device *isku = hid_get_drvdata(hdev);
392
393 if (intf->cur_altsetting->desc.bInterfaceProtocol
394 != ISKU_USB_INTERFACE_PROTOCOL)
395 return 0;
396
397 if (isku == NULL)
398 return 0;
399
400 isku_keep_values_up_to_date(isku, data);
401
402 if (isku->roccat_claimed)
403 isku_report_to_chrdev(isku, data);
404
405 return 0;
406}
407
408static const struct hid_device_id isku_devices[] = {
409 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
Stefan Achatzce716962013-03-10 12:33:02 +0100410 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKUFX) },
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100411 { }
412};
413
414MODULE_DEVICE_TABLE(hid, isku_devices);
415
416static struct hid_driver isku_driver = {
417 .name = "isku",
418 .id_table = isku_devices,
419 .probe = isku_probe,
420 .remove = isku_remove,
421 .raw_event = isku_raw_event
422};
423
424static int __init isku_init(void)
425{
426 int retval;
427 isku_class = class_create(THIS_MODULE, "isku");
428 if (IS_ERR(isku_class))
429 return PTR_ERR(isku_class);
430 isku_class->dev_attrs = isku_attributes;
431 isku_class->dev_bin_attrs = isku_bin_attributes;
432
433 retval = hid_register_driver(&isku_driver);
434 if (retval)
435 class_destroy(isku_class);
436 return retval;
437}
438
439static void __exit isku_exit(void)
440{
441 hid_unregister_driver(&isku_driver);
442 class_destroy(isku_class);
443}
444
445module_init(isku_init);
446module_exit(isku_exit);
447
448MODULE_AUTHOR("Stefan Achatz");
Stefan Achatzce716962013-03-10 12:33:02 +0100449MODULE_DESCRIPTION("USB Roccat Isku/FX driver");
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100450MODULE_LICENSE("GPL v2");