blob: 3983dec062d7ef1c8a62a6886db3a3d73d9d3b02 [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}
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};
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700119ATTRIBUTE_GROUPS(isku);
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100120
121static ssize_t isku_sysfs_read(struct file *fp, struct kobject *kobj,
122 char *buf, loff_t off, size_t count,
123 size_t real_size, uint command)
124{
125 struct device *dev =
126 container_of(kobj, struct device, kobj)->parent->parent;
127 struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
128 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
129 int retval;
130
131 if (off >= real_size)
132 return 0;
133
Stefan Achatzce716962013-03-10 12:33:02 +0100134 if (off != 0 || count > real_size)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100135 return -EINVAL;
136
137 mutex_lock(&isku->isku_lock);
Stefan Achatzce716962013-03-10 12:33:02 +0100138 retval = isku_receive(usb_dev, command, buf, count);
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100139 mutex_unlock(&isku->isku_lock);
140
Stefan Achatzce716962013-03-10 12:33:02 +0100141 return retval ? retval : count;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100142}
143
144static ssize_t isku_sysfs_write(struct file *fp, struct kobject *kobj,
145 void const *buf, loff_t off, size_t count,
146 size_t real_size, uint command)
147{
148 struct device *dev =
149 container_of(kobj, struct device, kobj)->parent->parent;
150 struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
151 struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
152 int retval;
153
Stefan Achatzce716962013-03-10 12:33:02 +0100154 if (off != 0 || count > real_size)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100155 return -EINVAL;
156
157 mutex_lock(&isku->isku_lock);
Stefan Achatz7392d732012-05-20 22:45:04 +0200158 retval = roccat_common2_send_with_status(usb_dev, command,
Stefan Achatzce716962013-03-10 12:33:02 +0100159 (void *)buf, count);
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100160 mutex_unlock(&isku->isku_lock);
161
Stefan Achatzce716962013-03-10 12:33:02 +0100162 return retval ? retval : count;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100163}
164
165#define ISKU_SYSFS_W(thingy, THINGY) \
166static ssize_t isku_sysfs_write_ ## thingy(struct file *fp, struct kobject *kobj, \
167 struct bin_attribute *attr, char *buf, \
168 loff_t off, size_t count) \
169{ \
170 return isku_sysfs_write(fp, kobj, buf, off, count, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100171 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100172}
173
174#define ISKU_SYSFS_R(thingy, THINGY) \
175static ssize_t isku_sysfs_read_ ## thingy(struct file *fp, struct kobject *kobj, \
176 struct bin_attribute *attr, char *buf, \
177 loff_t off, size_t count) \
178{ \
179 return isku_sysfs_read(fp, kobj, buf, off, count, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100180 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100181}
182
183#define ISKU_SYSFS_RW(thingy, THINGY) \
184ISKU_SYSFS_R(thingy, THINGY) \
185ISKU_SYSFS_W(thingy, THINGY)
186
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100187#define ISKU_BIN_ATTR_RW(thingy, THINGY) \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100188{ \
189 .attr = { .name = #thingy, .mode = 0660 }, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100190 .size = ISKU_SIZE_ ## THINGY, \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100191 .read = isku_sysfs_read_ ## thingy, \
192 .write = isku_sysfs_write_ ## thingy \
193}
194
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100195#define ISKU_BIN_ATTR_R(thingy, THINGY) \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100196{ \
197 .attr = { .name = #thingy, .mode = 0440 }, \
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100198 .size = ISKU_SIZE_ ## THINGY, \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100199 .read = isku_sysfs_read_ ## thingy, \
200}
201
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100202#define ISKU_BIN_ATTR_W(thingy, THINGY) \
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100203{ \
204 .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
209ISKU_SYSFS_RW(macro, MACRO)
210ISKU_SYSFS_RW(keys_function, KEYS_FUNCTION)
211ISKU_SYSFS_RW(keys_easyzone, KEYS_EASYZONE)
212ISKU_SYSFS_RW(keys_media, KEYS_MEDIA)
213ISKU_SYSFS_RW(keys_thumbster, KEYS_THUMBSTER)
214ISKU_SYSFS_RW(keys_macro, KEYS_MACRO)
215ISKU_SYSFS_RW(keys_capslock, KEYS_CAPSLOCK)
216ISKU_SYSFS_RW(light, LIGHT)
217ISKU_SYSFS_RW(key_mask, KEY_MASK)
218ISKU_SYSFS_RW(last_set, LAST_SET)
219ISKU_SYSFS_W(talk, TALK)
Stefan Achatzce716962013-03-10 12:33:02 +0100220ISKU_SYSFS_W(talkfx, TALKFX)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100221ISKU_SYSFS_R(info, INFO)
222ISKU_SYSFS_W(control, CONTROL)
Stefan Achatzf1da71e2012-11-11 06:21:06 +0100223ISKU_SYSFS_W(reset, RESET)
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100224
225static struct bin_attribute isku_bin_attributes[] = {
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100226 ISKU_BIN_ATTR_RW(macro, MACRO),
227 ISKU_BIN_ATTR_RW(keys_function, KEYS_FUNCTION),
228 ISKU_BIN_ATTR_RW(keys_easyzone, KEYS_EASYZONE),
229 ISKU_BIN_ATTR_RW(keys_media, KEYS_MEDIA),
230 ISKU_BIN_ATTR_RW(keys_thumbster, KEYS_THUMBSTER),
231 ISKU_BIN_ATTR_RW(keys_macro, KEYS_MACRO),
232 ISKU_BIN_ATTR_RW(keys_capslock, KEYS_CAPSLOCK),
233 ISKU_BIN_ATTR_RW(light, LIGHT),
234 ISKU_BIN_ATTR_RW(key_mask, KEY_MASK),
235 ISKU_BIN_ATTR_RW(last_set, LAST_SET),
236 ISKU_BIN_ATTR_W(talk, TALK),
Stefan Achatzce716962013-03-10 12:33:02 +0100237 ISKU_BIN_ATTR_W(talkfx, TALKFX),
Stefan Achatz6e5920d2012-11-11 06:21:02 +0100238 ISKU_BIN_ATTR_R(info, INFO),
239 ISKU_BIN_ATTR_W(control, CONTROL),
Stefan Achatzf1da71e2012-11-11 06:21:06 +0100240 ISKU_BIN_ATTR_W(reset, RESET),
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100241 __ATTR_NULL
242};
243
244static int isku_init_isku_device_struct(struct usb_device *usb_dev,
245 struct isku_device *isku)
246{
247 int retval;
248
249 mutex_init(&isku->isku_lock);
250
251 retval = isku_get_actual_profile(usb_dev);
252 if (retval < 0)
253 return retval;
254 isku_profile_activated(isku, retval);
255
256 return 0;
257}
258
259static int isku_init_specials(struct hid_device *hdev)
260{
261 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
262 struct usb_device *usb_dev = interface_to_usbdev(intf);
263 struct isku_device *isku;
264 int retval;
265
266 if (intf->cur_altsetting->desc.bInterfaceProtocol
267 != ISKU_USB_INTERFACE_PROTOCOL) {
268 hid_set_drvdata(hdev, NULL);
269 return 0;
270 }
271
272 isku = kzalloc(sizeof(*isku), GFP_KERNEL);
273 if (!isku) {
274 hid_err(hdev, "can't alloc device descriptor\n");
275 return -ENOMEM;
276 }
277 hid_set_drvdata(hdev, isku);
278
279 retval = isku_init_isku_device_struct(usb_dev, isku);
280 if (retval) {
281 hid_err(hdev, "couldn't init struct isku_device\n");
282 goto exit_free;
283 }
284
285 retval = roccat_connect(isku_class, hdev,
286 sizeof(struct isku_roccat_report));
287 if (retval < 0) {
288 hid_err(hdev, "couldn't init char dev\n");
289 } else {
290 isku->chrdev_minor = retval;
291 isku->roccat_claimed = 1;
292 }
293
294 return 0;
295exit_free:
296 kfree(isku);
297 return retval;
298}
299
300static void isku_remove_specials(struct hid_device *hdev)
301{
302 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
303 struct isku_device *isku;
304
305 if (intf->cur_altsetting->desc.bInterfaceProtocol
306 != ISKU_USB_INTERFACE_PROTOCOL)
307 return;
308
309 isku = hid_get_drvdata(hdev);
310 if (isku->roccat_claimed)
311 roccat_disconnect(isku->chrdev_minor);
312 kfree(isku);
313}
314
315static int isku_probe(struct hid_device *hdev,
316 const struct hid_device_id *id)
317{
318 int retval;
319
320 retval = hid_parse(hdev);
321 if (retval) {
322 hid_err(hdev, "parse failed\n");
323 goto exit;
324 }
325
326 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
327 if (retval) {
328 hid_err(hdev, "hw start failed\n");
329 goto exit;
330 }
331
332 retval = isku_init_specials(hdev);
333 if (retval) {
334 hid_err(hdev, "couldn't install keyboard\n");
335 goto exit_stop;
336 }
337
338 return 0;
339
340exit_stop:
341 hid_hw_stop(hdev);
342exit:
343 return retval;
344}
345
346static void isku_remove(struct hid_device *hdev)
347{
348 isku_remove_specials(hdev);
349 hid_hw_stop(hdev);
350}
351
352static void isku_keep_values_up_to_date(struct isku_device *isku,
353 u8 const *data)
354{
355 struct isku_report_button const *button_report;
356
357 switch (data[0]) {
358 case ISKU_REPORT_NUMBER_BUTTON:
359 button_report = (struct isku_report_button const *)data;
360 switch (button_report->event) {
361 case ISKU_REPORT_BUTTON_EVENT_PROFILE:
362 isku_profile_activated(isku, button_report->data1 - 1);
363 break;
364 }
365 break;
366 }
367}
368
369static void isku_report_to_chrdev(struct isku_device const *isku,
370 u8 const *data)
371{
372 struct isku_roccat_report roccat_report;
373 struct isku_report_button const *button_report;
374
375 if (data[0] != ISKU_REPORT_NUMBER_BUTTON)
376 return;
377
378 button_report = (struct isku_report_button const *)data;
379
380 roccat_report.event = button_report->event;
381 roccat_report.data1 = button_report->data1;
382 roccat_report.data2 = button_report->data2;
383 roccat_report.profile = isku->actual_profile + 1;
384 roccat_report_event(isku->chrdev_minor,
385 (uint8_t const *)&roccat_report);
386}
387
388static int isku_raw_event(struct hid_device *hdev,
389 struct hid_report *report, u8 *data, int size)
390{
391 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
392 struct isku_device *isku = hid_get_drvdata(hdev);
393
394 if (intf->cur_altsetting->desc.bInterfaceProtocol
395 != ISKU_USB_INTERFACE_PROTOCOL)
396 return 0;
397
398 if (isku == NULL)
399 return 0;
400
401 isku_keep_values_up_to_date(isku, data);
402
403 if (isku->roccat_claimed)
404 isku_report_to_chrdev(isku, data);
405
406 return 0;
407}
408
409static const struct hid_device_id isku_devices[] = {
410 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
Stefan Achatzce716962013-03-10 12:33:02 +0100411 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKUFX) },
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100412 { }
413};
414
415MODULE_DEVICE_TABLE(hid, isku_devices);
416
417static struct hid_driver isku_driver = {
418 .name = "isku",
419 .id_table = isku_devices,
420 .probe = isku_probe,
421 .remove = isku_remove,
422 .raw_event = isku_raw_event
423};
424
425static int __init isku_init(void)
426{
427 int retval;
428 isku_class = class_create(THIS_MODULE, "isku");
429 if (IS_ERR(isku_class))
430 return PTR_ERR(isku_class);
Greg Kroah-Hartman46a58c42013-07-24 15:05:11 -0700431 isku_class->dev_groups = isku_groups;
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100432 isku_class->dev_bin_attrs = isku_bin_attributes;
433
434 retval = hid_register_driver(&isku_driver);
435 if (retval)
436 class_destroy(isku_class);
437 return retval;
438}
439
440static void __exit isku_exit(void)
441{
442 hid_unregister_driver(&isku_driver);
443 class_destroy(isku_class);
444}
445
446module_init(isku_init);
447module_exit(isku_exit);
448
449MODULE_AUTHOR("Stefan Achatz");
Stefan Achatzce716962013-03-10 12:33:02 +0100450MODULE_DESCRIPTION("USB Roccat Isku/FX driver");
Stefan Achatzd41c2a72011-11-24 17:46:24 +0100451MODULE_LICENSE("GPL v2");