blob: 02db537f8f3eaf79e50ed9126559735d7c6e388d [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
Jingoo Handfc450b2013-07-19 15:53:16 +090085 retval = kstrtoul(buf, 10, &profile);
Stefan Achatzd41c2a72011-11-24 17:46:24 +010086 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}
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700112static DEVICE_ATTR(actual_profile, 0660, isku_sysfs_show_actual_profile,
113 isku_sysfs_set_actual_profile);
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100114
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700115static struct attribute *isku_attrs[] = {
116 &dev_attr_actual_profile.attr,
117 NULL,
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100118};
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{
Geliang Tang2cf83832015-12-27 17:25:24 +0800124 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100125 struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
126 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
127 int retval;
128
129 if (off >= real_size)
130 return 0;
131
Stefan Achatzce716962013-03-10 12:33:02 +0100132 if (off != 0 || count > real_size)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100133 return -EINVAL;
134
135 mutex_lock(&isku->isku_lock);
Stefan Achatzce716962013-03-10 12:33:02 +0100136 retval = isku_receive(usb_dev, command, buf, count);
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100137 mutex_unlock(&isku->isku_lock);
138
Stefan Achatzce716962013-03-10 12:33:02 +0100139 return retval ? retval : count;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100140}
141
142static ssize_t isku_sysfs_write(struct file *fp, struct kobject *kobj,
143 void const *buf, loff_t off, size_t count,
144 size_t real_size, uint command)
145{
Geliang Tang2cf83832015-12-27 17:25:24 +0800146 struct device *dev = kobj_to_dev(kobj)->parent->parent;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100147 struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
148 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
149 int retval;
150
Stefan Achatzce716962013-03-10 12:33:02 +0100151 if (off != 0 || count > real_size)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100152 return -EINVAL;
153
154 mutex_lock(&isku->isku_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200155 retval = roccat_common2_send_with_status(usb_dev, command,
Stefan Achatzce716962013-03-10 12:33:02 +0100156 (void *)buf, count);
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100157 mutex_unlock(&isku->isku_lock);
158
Stefan Achatzce716962013-03-10 12:33:02 +0100159 return retval ? retval : count;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100160}
161
162#define ISKU_SYSFS_W(thingy, THINGY) \
163static ssize_t isku_sysfs_write_ ## thingy(struct file *fp, struct kobject *kobj, \
164 struct bin_attribute *attr, char *buf, \
165 loff_t off, size_t count) \
166{ \
167 return isku_sysfs_write(fp, kobj, buf, off, count, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100168 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100169}
170
171#define ISKU_SYSFS_R(thingy, THINGY) \
172static ssize_t isku_sysfs_read_ ## thingy(struct file *fp, struct kobject *kobj, \
173 struct bin_attribute *attr, char *buf, \
174 loff_t off, size_t count) \
175{ \
176 return isku_sysfs_read(fp, kobj, buf, off, count, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100177 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100178}
179
180#define ISKU_SYSFS_RW(thingy, THINGY) \
181ISKU_SYSFS_R(thingy, THINGY) \
182ISKU_SYSFS_W(thingy, THINGY)
183
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100184#define ISKU_BIN_ATTR_RW(thingy, THINGY) \
Greg Kroah-Hartman8daf8c32013-08-19 21:40:26 -0700185ISKU_SYSFS_RW(thingy, THINGY); \
186static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100187 .attr = { .name = #thingy, .mode = 0660 }, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100188 .size = ISKU_SIZE_ ## THINGY, \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100189 .read = isku_sysfs_read_ ## thingy, \
190 .write = isku_sysfs_write_ ## thingy \
191}
192
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100193#define ISKU_BIN_ATTR_R(thingy, THINGY) \
Greg Kroah-Hartman8daf8c32013-08-19 21:40:26 -0700194ISKU_SYSFS_R(thingy, THINGY); \
195static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100196 .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) \
Greg Kroah-Hartman8daf8c32013-08-19 21:40:26 -0700202ISKU_SYSFS_W(thingy, THINGY); \
203static struct bin_attribute bin_attr_##thingy = { \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100204 .attr = { .name = #thingy, .mode = 0220 }, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100205 .size = ISKU_SIZE_ ## THINGY, \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100206 .write = isku_sysfs_write_ ## thingy \
207}
208
Greg Kroah-Hartman8daf8c32013-08-19 21:40:26 -0700209ISKU_BIN_ATTR_RW(macro, MACRO);
210ISKU_BIN_ATTR_RW(keys_function, KEYS_FUNCTION);
211ISKU_BIN_ATTR_RW(keys_easyzone, KEYS_EASYZONE);
212ISKU_BIN_ATTR_RW(keys_media, KEYS_MEDIA);
213ISKU_BIN_ATTR_RW(keys_thumbster, KEYS_THUMBSTER);
214ISKU_BIN_ATTR_RW(keys_macro, KEYS_MACRO);
215ISKU_BIN_ATTR_RW(keys_capslock, KEYS_CAPSLOCK);
216ISKU_BIN_ATTR_RW(light, LIGHT);
217ISKU_BIN_ATTR_RW(key_mask, KEY_MASK);
218ISKU_BIN_ATTR_RW(last_set, LAST_SET);
219ISKU_BIN_ATTR_W(talk, TALK);
220ISKU_BIN_ATTR_W(talkfx, TALKFX);
221ISKU_BIN_ATTR_W(control, CONTROL);
222ISKU_BIN_ATTR_W(reset, RESET);
223ISKU_BIN_ATTR_R(info, INFO);
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100224
Greg Kroah-Hartman8daf8c32013-08-19 21:40:26 -0700225static struct bin_attribute *isku_bin_attributes[] = {
226 &bin_attr_macro,
227 &bin_attr_keys_function,
228 &bin_attr_keys_easyzone,
229 &bin_attr_keys_media,
230 &bin_attr_keys_thumbster,
231 &bin_attr_keys_macro,
232 &bin_attr_keys_capslock,
233 &bin_attr_light,
234 &bin_attr_key_mask,
235 &bin_attr_last_set,
236 &bin_attr_talk,
237 &bin_attr_talkfx,
238 &bin_attr_control,
239 &bin_attr_reset,
240 &bin_attr_info,
241 NULL,
242};
243
244static const struct attribute_group isku_group = {
245 .attrs = isku_attrs,
246 .bin_attrs = isku_bin_attributes,
247};
248
249static const struct attribute_group *isku_groups[] = {
250 &isku_group,
251 NULL,
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100252};
253
254static int isku_init_isku_device_struct(struct usb_device *usb_dev,
255 struct isku_device *isku)
256{
257 int retval;
258
259 mutex_init(&isku->isku_lock);
260
261 retval = isku_get_actual_profile(usb_dev);
262 if (retval < 0)
263 return retval;
264 isku_profile_activated(isku, retval);
265
266 return 0;
267}
268
269static int isku_init_specials(struct hid_device *hdev)
270{
271 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
272 struct usb_device *usb_dev = interface_to_usbdev(intf);
273 struct isku_device *isku;
274 int retval;
275
276 if (intf->cur_altsetting->desc.bInterfaceProtocol
277 != ISKU_USB_INTERFACE_PROTOCOL) {
278 hid_set_drvdata(hdev, NULL);
279 return 0;
280 }
281
282 isku = kzalloc(sizeof(*isku), GFP_KERNEL);
283 if (!isku) {
284 hid_err(hdev, "can't alloc device descriptor\n");
285 return -ENOMEM;
286 }
287 hid_set_drvdata(hdev, isku);
288
289 retval = isku_init_isku_device_struct(usb_dev, isku);
290 if (retval) {
291 hid_err(hdev, "couldn't init struct isku_device\n");
292 goto exit_free;
293 }
294
295 retval = roccat_connect(isku_class, hdev,
296 sizeof(struct isku_roccat_report));
297 if (retval < 0) {
298 hid_err(hdev, "couldn't init char dev\n");
299 } else {
300 isku->chrdev_minor = retval;
301 isku->roccat_claimed = 1;
302 }
303
304 return 0;
305exit_free:
306 kfree(isku);
307 return retval;
308}
309
310static void isku_remove_specials(struct hid_device *hdev)
311{
312 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
313 struct isku_device *isku;
314
315 if (intf->cur_altsetting->desc.bInterfaceProtocol
316 != ISKU_USB_INTERFACE_PROTOCOL)
317 return;
318
319 isku = hid_get_drvdata(hdev);
320 if (isku->roccat_claimed)
321 roccat_disconnect(isku->chrdev_minor);
322 kfree(isku);
323}
324
325static int isku_probe(struct hid_device *hdev,
326 const struct hid_device_id *id)
327{
328 int retval;
329
330 retval = hid_parse(hdev);
331 if (retval) {
332 hid_err(hdev, "parse failed\n");
333 goto exit;
334 }
335
336 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
337 if (retval) {
338 hid_err(hdev, "hw start failed\n");
339 goto exit;
340 }
341
342 retval = isku_init_specials(hdev);
343 if (retval) {
344 hid_err(hdev, "couldn't install keyboard\n");
345 goto exit_stop;
346 }
347
348 return 0;
349
350exit_stop:
351 hid_hw_stop(hdev);
352exit:
353 return retval;
354}
355
356static void isku_remove(struct hid_device *hdev)
357{
358 isku_remove_specials(hdev);
359 hid_hw_stop(hdev);
360}
361
362static void isku_keep_values_up_to_date(struct isku_device *isku,
363 u8 const *data)
364{
365 struct isku_report_button const *button_report;
366
367 switch (data[0]) {
368 case ISKU_REPORT_NUMBER_BUTTON:
369 button_report = (struct isku_report_button const *)data;
370 switch (button_report->event) {
371 case ISKU_REPORT_BUTTON_EVENT_PROFILE:
372 isku_profile_activated(isku, button_report->data1 - 1);
373 break;
374 }
375 break;
376 }
377}
378
379static void isku_report_to_chrdev(struct isku_device const *isku,
380 u8 const *data)
381{
382 struct isku_roccat_report roccat_report;
383 struct isku_report_button const *button_report;
384
385 if (data[0] != ISKU_REPORT_NUMBER_BUTTON)
386 return;
387
388 button_report = (struct isku_report_button const *)data;
389
390 roccat_report.event = button_report->event;
391 roccat_report.data1 = button_report->data1;
392 roccat_report.data2 = button_report->data2;
393 roccat_report.profile = isku->actual_profile + 1;
394 roccat_report_event(isku->chrdev_minor,
395 (uint8_t const *)&roccat_report);
396}
397
398static int isku_raw_event(struct hid_device *hdev,
399 struct hid_report *report, u8 *data, int size)
400{
401 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
402 struct isku_device *isku = hid_get_drvdata(hdev);
403
404 if (intf->cur_altsetting->desc.bInterfaceProtocol
405 != ISKU_USB_INTERFACE_PROTOCOL)
406 return 0;
407
408 if (isku == NULL)
409 return 0;
410
411 isku_keep_values_up_to_date(isku, data);
412
413 if (isku->roccat_claimed)
414 isku_report_to_chrdev(isku, data);
415
416 return 0;
417}
418
419static const struct hid_device_id isku_devices[] = {
420 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
Stefan Achatzce716962013-03-10 12:33:02 +0100421 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKUFX) },
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100422 { }
423};
424
425MODULE_DEVICE_TABLE(hid, isku_devices);
426
427static struct hid_driver isku_driver = {
428 .name = "isku",
429 .id_table = isku_devices,
430 .probe = isku_probe,
431 .remove = isku_remove,
432 .raw_event = isku_raw_event
433};
434
435static int __init isku_init(void)
436{
437 int retval;
438 isku_class = class_create(THIS_MODULE, "isku");
439 if (IS_ERR(isku_class))
440 return PTR_ERR(isku_class);
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700441 isku_class->dev_groups = isku_groups;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100442
443 retval = hid_register_driver(&isku_driver);
444 if (retval)
445 class_destroy(isku_class);
446 return retval;
447}
448
449static void __exit isku_exit(void)
450{
451 hid_unregister_driver(&isku_driver);
452 class_destroy(isku_class);
453}
454
455module_init(isku_init);
456module_exit(isku_exit);
457
458MODULE_AUTHOR("Stefan Achatz");
Stefan Achatzce716962013-03-10 12:33:02 +0100459MODULE_DESCRIPTION("USB Roccat Isku/FX driver");
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100460MODULE_LICENSE("GPL v2");