]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/hid/hid-roccat-isku.c
HID: input: ignore the battery in OKLICK Laser BTmouse
[mirror_ubuntu-bionic-kernel.git] / drivers / hid / hid-roccat-isku.c
1 /*
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
29 static struct class *isku_class;
30
31 static void isku_profile_activated(struct isku_device *isku, uint new_profile)
32 {
33 isku->actual_profile = new_profile;
34 }
35
36 static int isku_receive(struct usb_device *usb_dev, uint command,
37 void *buf, uint size)
38 {
39 return roccat_common2_receive(usb_dev, command, buf, size);
40 }
41
42 static 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
52 static 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;
59 return roccat_common2_send_with_status(usb_dev,
60 ISKU_COMMAND_ACTUAL_PROFILE, &buf,
61 sizeof(struct isku_actual_profile));
62 }
63
64 static 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
72 static 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 = kstrtoul(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 static DEVICE_ATTR(actual_profile, 0660, isku_sysfs_show_actual_profile,
113 isku_sysfs_set_actual_profile);
114
115 static struct attribute *isku_attrs[] = {
116 &dev_attr_actual_profile.attr,
117 NULL,
118 };
119
120 static 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
133 if (off != 0 || count > real_size)
134 return -EINVAL;
135
136 mutex_lock(&isku->isku_lock);
137 retval = isku_receive(usb_dev, command, buf, count);
138 mutex_unlock(&isku->isku_lock);
139
140 return retval ? retval : count;
141 }
142
143 static 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
153 if (off != 0 || count > real_size)
154 return -EINVAL;
155
156 mutex_lock(&isku->isku_lock);
157 retval = roccat_common2_send_with_status(usb_dev, command,
158 (void *)buf, count);
159 mutex_unlock(&isku->isku_lock);
160
161 return retval ? retval : count;
162 }
163
164 #define ISKU_SYSFS_W(thingy, THINGY) \
165 static 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, \
170 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
171 }
172
173 #define ISKU_SYSFS_R(thingy, THINGY) \
174 static 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, \
179 ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
180 }
181
182 #define ISKU_SYSFS_RW(thingy, THINGY) \
183 ISKU_SYSFS_R(thingy, THINGY) \
184 ISKU_SYSFS_W(thingy, THINGY)
185
186 #define ISKU_BIN_ATTR_RW(thingy, THINGY) \
187 ISKU_SYSFS_RW(thingy, THINGY); \
188 static struct bin_attribute bin_attr_##thingy = { \
189 .attr = { .name = #thingy, .mode = 0660 }, \
190 .size = ISKU_SIZE_ ## THINGY, \
191 .read = isku_sysfs_read_ ## thingy, \
192 .write = isku_sysfs_write_ ## thingy \
193 }
194
195 #define ISKU_BIN_ATTR_R(thingy, THINGY) \
196 ISKU_SYSFS_R(thingy, THINGY); \
197 static struct bin_attribute bin_attr_##thingy = { \
198 .attr = { .name = #thingy, .mode = 0440 }, \
199 .size = ISKU_SIZE_ ## THINGY, \
200 .read = isku_sysfs_read_ ## thingy, \
201 }
202
203 #define ISKU_BIN_ATTR_W(thingy, THINGY) \
204 ISKU_SYSFS_W(thingy, THINGY); \
205 static struct bin_attribute bin_attr_##thingy = { \
206 .attr = { .name = #thingy, .mode = 0220 }, \
207 .size = ISKU_SIZE_ ## THINGY, \
208 .write = isku_sysfs_write_ ## thingy \
209 }
210
211 ISKU_BIN_ATTR_RW(macro, MACRO);
212 ISKU_BIN_ATTR_RW(keys_function, KEYS_FUNCTION);
213 ISKU_BIN_ATTR_RW(keys_easyzone, KEYS_EASYZONE);
214 ISKU_BIN_ATTR_RW(keys_media, KEYS_MEDIA);
215 ISKU_BIN_ATTR_RW(keys_thumbster, KEYS_THUMBSTER);
216 ISKU_BIN_ATTR_RW(keys_macro, KEYS_MACRO);
217 ISKU_BIN_ATTR_RW(keys_capslock, KEYS_CAPSLOCK);
218 ISKU_BIN_ATTR_RW(light, LIGHT);
219 ISKU_BIN_ATTR_RW(key_mask, KEY_MASK);
220 ISKU_BIN_ATTR_RW(last_set, LAST_SET);
221 ISKU_BIN_ATTR_W(talk, TALK);
222 ISKU_BIN_ATTR_W(talkfx, TALKFX);
223 ISKU_BIN_ATTR_W(control, CONTROL);
224 ISKU_BIN_ATTR_W(reset, RESET);
225 ISKU_BIN_ATTR_R(info, INFO);
226
227 static struct bin_attribute *isku_bin_attributes[] = {
228 &bin_attr_macro,
229 &bin_attr_keys_function,
230 &bin_attr_keys_easyzone,
231 &bin_attr_keys_media,
232 &bin_attr_keys_thumbster,
233 &bin_attr_keys_macro,
234 &bin_attr_keys_capslock,
235 &bin_attr_light,
236 &bin_attr_key_mask,
237 &bin_attr_last_set,
238 &bin_attr_talk,
239 &bin_attr_talkfx,
240 &bin_attr_control,
241 &bin_attr_reset,
242 &bin_attr_info,
243 NULL,
244 };
245
246 static const struct attribute_group isku_group = {
247 .attrs = isku_attrs,
248 .bin_attrs = isku_bin_attributes,
249 };
250
251 static const struct attribute_group *isku_groups[] = {
252 &isku_group,
253 NULL,
254 };
255
256 static int isku_init_isku_device_struct(struct usb_device *usb_dev,
257 struct isku_device *isku)
258 {
259 int retval;
260
261 mutex_init(&isku->isku_lock);
262
263 retval = isku_get_actual_profile(usb_dev);
264 if (retval < 0)
265 return retval;
266 isku_profile_activated(isku, retval);
267
268 return 0;
269 }
270
271 static int isku_init_specials(struct hid_device *hdev)
272 {
273 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
274 struct usb_device *usb_dev = interface_to_usbdev(intf);
275 struct isku_device *isku;
276 int retval;
277
278 if (intf->cur_altsetting->desc.bInterfaceProtocol
279 != ISKU_USB_INTERFACE_PROTOCOL) {
280 hid_set_drvdata(hdev, NULL);
281 return 0;
282 }
283
284 isku = kzalloc(sizeof(*isku), GFP_KERNEL);
285 if (!isku) {
286 hid_err(hdev, "can't alloc device descriptor\n");
287 return -ENOMEM;
288 }
289 hid_set_drvdata(hdev, isku);
290
291 retval = isku_init_isku_device_struct(usb_dev, isku);
292 if (retval) {
293 hid_err(hdev, "couldn't init struct isku_device\n");
294 goto exit_free;
295 }
296
297 retval = roccat_connect(isku_class, hdev,
298 sizeof(struct isku_roccat_report));
299 if (retval < 0) {
300 hid_err(hdev, "couldn't init char dev\n");
301 } else {
302 isku->chrdev_minor = retval;
303 isku->roccat_claimed = 1;
304 }
305
306 return 0;
307 exit_free:
308 kfree(isku);
309 return retval;
310 }
311
312 static void isku_remove_specials(struct hid_device *hdev)
313 {
314 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
315 struct isku_device *isku;
316
317 if (intf->cur_altsetting->desc.bInterfaceProtocol
318 != ISKU_USB_INTERFACE_PROTOCOL)
319 return;
320
321 isku = hid_get_drvdata(hdev);
322 if (isku->roccat_claimed)
323 roccat_disconnect(isku->chrdev_minor);
324 kfree(isku);
325 }
326
327 static int isku_probe(struct hid_device *hdev,
328 const struct hid_device_id *id)
329 {
330 int retval;
331
332 retval = hid_parse(hdev);
333 if (retval) {
334 hid_err(hdev, "parse failed\n");
335 goto exit;
336 }
337
338 retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
339 if (retval) {
340 hid_err(hdev, "hw start failed\n");
341 goto exit;
342 }
343
344 retval = isku_init_specials(hdev);
345 if (retval) {
346 hid_err(hdev, "couldn't install keyboard\n");
347 goto exit_stop;
348 }
349
350 return 0;
351
352 exit_stop:
353 hid_hw_stop(hdev);
354 exit:
355 return retval;
356 }
357
358 static void isku_remove(struct hid_device *hdev)
359 {
360 isku_remove_specials(hdev);
361 hid_hw_stop(hdev);
362 }
363
364 static void isku_keep_values_up_to_date(struct isku_device *isku,
365 u8 const *data)
366 {
367 struct isku_report_button const *button_report;
368
369 switch (data[0]) {
370 case ISKU_REPORT_NUMBER_BUTTON:
371 button_report = (struct isku_report_button const *)data;
372 switch (button_report->event) {
373 case ISKU_REPORT_BUTTON_EVENT_PROFILE:
374 isku_profile_activated(isku, button_report->data1 - 1);
375 break;
376 }
377 break;
378 }
379 }
380
381 static void isku_report_to_chrdev(struct isku_device const *isku,
382 u8 const *data)
383 {
384 struct isku_roccat_report roccat_report;
385 struct isku_report_button const *button_report;
386
387 if (data[0] != ISKU_REPORT_NUMBER_BUTTON)
388 return;
389
390 button_report = (struct isku_report_button const *)data;
391
392 roccat_report.event = button_report->event;
393 roccat_report.data1 = button_report->data1;
394 roccat_report.data2 = button_report->data2;
395 roccat_report.profile = isku->actual_profile + 1;
396 roccat_report_event(isku->chrdev_minor,
397 (uint8_t const *)&roccat_report);
398 }
399
400 static int isku_raw_event(struct hid_device *hdev,
401 struct hid_report *report, u8 *data, int size)
402 {
403 struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
404 struct isku_device *isku = hid_get_drvdata(hdev);
405
406 if (intf->cur_altsetting->desc.bInterfaceProtocol
407 != ISKU_USB_INTERFACE_PROTOCOL)
408 return 0;
409
410 if (isku == NULL)
411 return 0;
412
413 isku_keep_values_up_to_date(isku, data);
414
415 if (isku->roccat_claimed)
416 isku_report_to_chrdev(isku, data);
417
418 return 0;
419 }
420
421 static const struct hid_device_id isku_devices[] = {
422 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
423 { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKUFX) },
424 { }
425 };
426
427 MODULE_DEVICE_TABLE(hid, isku_devices);
428
429 static struct hid_driver isku_driver = {
430 .name = "isku",
431 .id_table = isku_devices,
432 .probe = isku_probe,
433 .remove = isku_remove,
434 .raw_event = isku_raw_event
435 };
436
437 static int __init isku_init(void)
438 {
439 int retval;
440 isku_class = class_create(THIS_MODULE, "isku");
441 if (IS_ERR(isku_class))
442 return PTR_ERR(isku_class);
443 isku_class->dev_groups = isku_groups;
444
445 retval = hid_register_driver(&isku_driver);
446 if (retval)
447 class_destroy(isku_class);
448 return retval;
449 }
450
451 static void __exit isku_exit(void)
452 {
453 hid_unregister_driver(&isku_driver);
454 class_destroy(isku_class);
455 }
456
457 module_init(isku_init);
458 module_exit(isku_exit);
459
460 MODULE_AUTHOR("Stefan Achatz");
461 MODULE_DESCRIPTION("USB Roccat Isku/FX driver");
462 MODULE_LICENSE("GPL v2");