hid-roccat-koneplus.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Roccat Kone[+] driver for Linux
  4. *
  5. * Copyright (c) 2010 Stefan Achatz <erazor_de@users.sourceforge.net>
  6. */
  7. /*
  8. */
  9. /*
  10. * Roccat Kone[+] is an updated/improved version of the Kone with more memory
  11. * and functionality and without the non-standard behaviours the Kone had.
  12. * KoneXTD has same capabilities but updated sensor.
  13. */
  14. #include <linux/device.h>
  15. #include <linux/input.h>
  16. #include <linux/hid.h>
  17. #include <linux/module.h>
  18. #include <linux/slab.h>
  19. #include <linux/hid-roccat.h>
  20. #include "hid-ids.h"
  21. #include "hid-roccat-common.h"
  22. #include "hid-roccat-koneplus.h"
  23. static uint profile_numbers[5] = {0, 1, 2, 3, 4};
  24. static void koneplus_profile_activated(struct koneplus_device *koneplus,
  25. uint new_profile)
  26. {
  27. koneplus->actual_profile = new_profile;
  28. }
  29. static int koneplus_send_control(struct usb_device *usb_dev, uint value,
  30. enum koneplus_control_requests request)
  31. {
  32. struct roccat_common2_control control;
  33. if ((request == KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS ||
  34. request == KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS) &&
  35. value > 4)
  36. return -EINVAL;
  37. control.command = ROCCAT_COMMON_COMMAND_CONTROL;
  38. control.value = value;
  39. control.request = request;
  40. return roccat_common2_send_with_status(usb_dev,
  41. ROCCAT_COMMON_COMMAND_CONTROL,
  42. &control, sizeof(struct roccat_common2_control));
  43. }
  44. /* retval is 0-4 on success, < 0 on error */
  45. static int koneplus_get_actual_profile(struct usb_device *usb_dev)
  46. {
  47. struct koneplus_actual_profile buf;
  48. int retval;
  49. retval = roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_ACTUAL_PROFILE,
  50. &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
  51. return retval ? retval : buf.actual_profile;
  52. }
  53. static int koneplus_set_actual_profile(struct usb_device *usb_dev,
  54. int new_profile)
  55. {
  56. struct koneplus_actual_profile buf;
  57. buf.command = KONEPLUS_COMMAND_ACTUAL_PROFILE;
  58. buf.size = KONEPLUS_SIZE_ACTUAL_PROFILE;
  59. buf.actual_profile = new_profile;
  60. return roccat_common2_send_with_status(usb_dev,
  61. KONEPLUS_COMMAND_ACTUAL_PROFILE,
  62. &buf, KONEPLUS_SIZE_ACTUAL_PROFILE);
  63. }
  64. static ssize_t koneplus_sysfs_read(struct file *fp, struct kobject *kobj,
  65. char *buf, loff_t off, size_t count,
  66. size_t real_size, uint command)
  67. {
  68. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  69. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  70. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  71. int retval;
  72. if (off >= real_size)
  73. return 0;
  74. if (off != 0 || count != real_size)
  75. return -EINVAL;
  76. mutex_lock(&koneplus->koneplus_lock);
  77. retval = roccat_common2_receive(usb_dev, command, buf, real_size);
  78. mutex_unlock(&koneplus->koneplus_lock);
  79. if (retval)
  80. return retval;
  81. return real_size;
  82. }
  83. static ssize_t koneplus_sysfs_write(struct file *fp, struct kobject *kobj,
  84. void const *buf, loff_t off, size_t count,
  85. size_t real_size, uint command)
  86. {
  87. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  88. struct koneplus_device *koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  89. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  90. int retval;
  91. if (off != 0 || count != real_size)
  92. return -EINVAL;
  93. mutex_lock(&koneplus->koneplus_lock);
  94. retval = roccat_common2_send_with_status(usb_dev, command,
  95. buf, real_size);
  96. mutex_unlock(&koneplus->koneplus_lock);
  97. if (retval)
  98. return retval;
  99. return real_size;
  100. }
  101. #define KONEPLUS_SYSFS_W(thingy, THINGY) \
  102. static ssize_t koneplus_sysfs_write_ ## thingy(struct file *fp, \
  103. struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  104. loff_t off, size_t count) \
  105. { \
  106. return koneplus_sysfs_write(fp, kobj, buf, off, count, \
  107. KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
  108. }
  109. #define KONEPLUS_SYSFS_R(thingy, THINGY) \
  110. static ssize_t koneplus_sysfs_read_ ## thingy(struct file *fp, \
  111. struct kobject *kobj, struct bin_attribute *attr, char *buf, \
  112. loff_t off, size_t count) \
  113. { \
  114. return koneplus_sysfs_read(fp, kobj, buf, off, count, \
  115. KONEPLUS_SIZE_ ## THINGY, KONEPLUS_COMMAND_ ## THINGY); \
  116. }
  117. #define KONEPLUS_SYSFS_RW(thingy, THINGY) \
  118. KONEPLUS_SYSFS_W(thingy, THINGY) \
  119. KONEPLUS_SYSFS_R(thingy, THINGY)
  120. #define KONEPLUS_BIN_ATTRIBUTE_RW(thingy, THINGY) \
  121. KONEPLUS_SYSFS_RW(thingy, THINGY); \
  122. static struct bin_attribute bin_attr_##thingy = { \
  123. .attr = { .name = #thingy, .mode = 0660 }, \
  124. .size = KONEPLUS_SIZE_ ## THINGY, \
  125. .read = koneplus_sysfs_read_ ## thingy, \
  126. .write = koneplus_sysfs_write_ ## thingy \
  127. }
  128. #define KONEPLUS_BIN_ATTRIBUTE_R(thingy, THINGY) \
  129. KONEPLUS_SYSFS_R(thingy, THINGY); \
  130. static struct bin_attribute bin_attr_##thingy = { \
  131. .attr = { .name = #thingy, .mode = 0440 }, \
  132. .size = KONEPLUS_SIZE_ ## THINGY, \
  133. .read = koneplus_sysfs_read_ ## thingy, \
  134. }
  135. #define KONEPLUS_BIN_ATTRIBUTE_W(thingy, THINGY) \
  136. KONEPLUS_SYSFS_W(thingy, THINGY); \
  137. static struct bin_attribute bin_attr_##thingy = { \
  138. .attr = { .name = #thingy, .mode = 0220 }, \
  139. .size = KONEPLUS_SIZE_ ## THINGY, \
  140. .write = koneplus_sysfs_write_ ## thingy \
  141. }
  142. KONEPLUS_BIN_ATTRIBUTE_W(control, CONTROL);
  143. KONEPLUS_BIN_ATTRIBUTE_W(talk, TALK);
  144. KONEPLUS_BIN_ATTRIBUTE_W(macro, MACRO);
  145. KONEPLUS_BIN_ATTRIBUTE_R(tcu_image, TCU_IMAGE);
  146. KONEPLUS_BIN_ATTRIBUTE_RW(info, INFO);
  147. KONEPLUS_BIN_ATTRIBUTE_RW(sensor, SENSOR);
  148. KONEPLUS_BIN_ATTRIBUTE_RW(tcu, TCU);
  149. KONEPLUS_BIN_ATTRIBUTE_RW(profile_settings, PROFILE_SETTINGS);
  150. KONEPLUS_BIN_ATTRIBUTE_RW(profile_buttons, PROFILE_BUTTONS);
  151. static ssize_t koneplus_sysfs_read_profilex_settings(struct file *fp,
  152. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  153. loff_t off, size_t count)
  154. {
  155. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  156. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  157. ssize_t retval;
  158. retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
  159. KONEPLUS_CONTROL_REQUEST_PROFILE_SETTINGS);
  160. if (retval)
  161. return retval;
  162. return koneplus_sysfs_read(fp, kobj, buf, off, count,
  163. KONEPLUS_SIZE_PROFILE_SETTINGS,
  164. KONEPLUS_COMMAND_PROFILE_SETTINGS);
  165. }
  166. static ssize_t koneplus_sysfs_read_profilex_buttons(struct file *fp,
  167. struct kobject *kobj, struct bin_attribute *attr, char *buf,
  168. loff_t off, size_t count)
  169. {
  170. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  171. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  172. ssize_t retval;
  173. retval = koneplus_send_control(usb_dev, *(uint *)(attr->private),
  174. KONEPLUS_CONTROL_REQUEST_PROFILE_BUTTONS);
  175. if (retval)
  176. return retval;
  177. return koneplus_sysfs_read(fp, kobj, buf, off, count,
  178. KONEPLUS_SIZE_PROFILE_BUTTONS,
  179. KONEPLUS_COMMAND_PROFILE_BUTTONS);
  180. }
  181. #define PROFILE_ATTR(number) \
  182. static struct bin_attribute bin_attr_profile##number##_settings = { \
  183. .attr = { .name = "profile" #number "_settings", .mode = 0440 }, \
  184. .size = KONEPLUS_SIZE_PROFILE_SETTINGS, \
  185. .read = koneplus_sysfs_read_profilex_settings, \
  186. .private = &profile_numbers[number-1], \
  187. }; \
  188. static struct bin_attribute bin_attr_profile##number##_buttons = { \
  189. .attr = { .name = "profile" #number "_buttons", .mode = 0440 }, \
  190. .size = KONEPLUS_SIZE_PROFILE_BUTTONS, \
  191. .read = koneplus_sysfs_read_profilex_buttons, \
  192. .private = &profile_numbers[number-1], \
  193. };
  194. PROFILE_ATTR(1);
  195. PROFILE_ATTR(2);
  196. PROFILE_ATTR(3);
  197. PROFILE_ATTR(4);
  198. PROFILE_ATTR(5);
  199. static ssize_t koneplus_sysfs_show_actual_profile(struct device *dev,
  200. struct device_attribute *attr, char *buf)
  201. {
  202. struct koneplus_device *koneplus =
  203. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  204. return sysfs_emit(buf, "%d\n", koneplus->actual_profile);
  205. }
  206. static ssize_t koneplus_sysfs_set_actual_profile(struct device *dev,
  207. struct device_attribute *attr, char const *buf, size_t size)
  208. {
  209. struct koneplus_device *koneplus;
  210. struct usb_device *usb_dev;
  211. unsigned long profile;
  212. int retval;
  213. struct koneplus_roccat_report roccat_report;
  214. dev = dev->parent->parent;
  215. koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  216. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  217. retval = kstrtoul(buf, 10, &profile);
  218. if (retval)
  219. return retval;
  220. if (profile > 4)
  221. return -EINVAL;
  222. mutex_lock(&koneplus->koneplus_lock);
  223. retval = koneplus_set_actual_profile(usb_dev, profile);
  224. if (retval) {
  225. mutex_unlock(&koneplus->koneplus_lock);
  226. return retval;
  227. }
  228. koneplus_profile_activated(koneplus, profile);
  229. roccat_report.type = KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE;
  230. roccat_report.data1 = profile + 1;
  231. roccat_report.data2 = 0;
  232. roccat_report.profile = profile + 1;
  233. roccat_report_event(koneplus->chrdev_minor,
  234. (uint8_t const *)&roccat_report);
  235. mutex_unlock(&koneplus->koneplus_lock);
  236. return size;
  237. }
  238. static DEVICE_ATTR(actual_profile, 0660,
  239. koneplus_sysfs_show_actual_profile,
  240. koneplus_sysfs_set_actual_profile);
  241. static DEVICE_ATTR(startup_profile, 0660,
  242. koneplus_sysfs_show_actual_profile,
  243. koneplus_sysfs_set_actual_profile);
  244. static ssize_t koneplus_sysfs_show_firmware_version(struct device *dev,
  245. struct device_attribute *attr, char *buf)
  246. {
  247. struct koneplus_device *koneplus;
  248. struct usb_device *usb_dev;
  249. struct koneplus_info info;
  250. dev = dev->parent->parent;
  251. koneplus = hid_get_drvdata(dev_get_drvdata(dev));
  252. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  253. mutex_lock(&koneplus->koneplus_lock);
  254. roccat_common2_receive(usb_dev, KONEPLUS_COMMAND_INFO,
  255. &info, KONEPLUS_SIZE_INFO);
  256. mutex_unlock(&koneplus->koneplus_lock);
  257. return sysfs_emit(buf, "%d\n", info.firmware_version);
  258. }
  259. static DEVICE_ATTR(firmware_version, 0440,
  260. koneplus_sysfs_show_firmware_version, NULL);
  261. static struct attribute *koneplus_attrs[] = {
  262. &dev_attr_actual_profile.attr,
  263. &dev_attr_startup_profile.attr,
  264. &dev_attr_firmware_version.attr,
  265. NULL,
  266. };
  267. static struct bin_attribute *koneplus_bin_attributes[] = {
  268. &bin_attr_control,
  269. &bin_attr_talk,
  270. &bin_attr_macro,
  271. &bin_attr_tcu_image,
  272. &bin_attr_info,
  273. &bin_attr_sensor,
  274. &bin_attr_tcu,
  275. &bin_attr_profile_settings,
  276. &bin_attr_profile_buttons,
  277. &bin_attr_profile1_settings,
  278. &bin_attr_profile2_settings,
  279. &bin_attr_profile3_settings,
  280. &bin_attr_profile4_settings,
  281. &bin_attr_profile5_settings,
  282. &bin_attr_profile1_buttons,
  283. &bin_attr_profile2_buttons,
  284. &bin_attr_profile3_buttons,
  285. &bin_attr_profile4_buttons,
  286. &bin_attr_profile5_buttons,
  287. NULL,
  288. };
  289. static const struct attribute_group koneplus_group = {
  290. .attrs = koneplus_attrs,
  291. .bin_attrs = koneplus_bin_attributes,
  292. };
  293. static const struct attribute_group *koneplus_groups[] = {
  294. &koneplus_group,
  295. NULL,
  296. };
  297. static const struct class koneplus_class = {
  298. .name = "koneplus",
  299. .dev_groups = koneplus_groups,
  300. };
  301. static int koneplus_init_koneplus_device_struct(struct usb_device *usb_dev,
  302. struct koneplus_device *koneplus)
  303. {
  304. int retval;
  305. mutex_init(&koneplus->koneplus_lock);
  306. retval = koneplus_get_actual_profile(usb_dev);
  307. if (retval < 0)
  308. return retval;
  309. koneplus_profile_activated(koneplus, retval);
  310. return 0;
  311. }
  312. static int koneplus_init_specials(struct hid_device *hdev)
  313. {
  314. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  315. struct usb_device *usb_dev = interface_to_usbdev(intf);
  316. struct koneplus_device *koneplus;
  317. int retval;
  318. if (intf->cur_altsetting->desc.bInterfaceProtocol
  319. == USB_INTERFACE_PROTOCOL_MOUSE) {
  320. koneplus = kzalloc(sizeof(*koneplus), GFP_KERNEL);
  321. if (!koneplus) {
  322. hid_err(hdev, "can't alloc device descriptor\n");
  323. return -ENOMEM;
  324. }
  325. hid_set_drvdata(hdev, koneplus);
  326. retval = koneplus_init_koneplus_device_struct(usb_dev, koneplus);
  327. if (retval) {
  328. hid_err(hdev, "couldn't init struct koneplus_device\n");
  329. goto exit_free;
  330. }
  331. retval = roccat_connect(&koneplus_class, hdev,
  332. sizeof(struct koneplus_roccat_report));
  333. if (retval < 0) {
  334. hid_err(hdev, "couldn't init char dev\n");
  335. } else {
  336. koneplus->chrdev_minor = retval;
  337. koneplus->roccat_claimed = 1;
  338. }
  339. } else {
  340. hid_set_drvdata(hdev, NULL);
  341. }
  342. return 0;
  343. exit_free:
  344. kfree(koneplus);
  345. return retval;
  346. }
  347. static void koneplus_remove_specials(struct hid_device *hdev)
  348. {
  349. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  350. struct koneplus_device *koneplus;
  351. if (intf->cur_altsetting->desc.bInterfaceProtocol
  352. == USB_INTERFACE_PROTOCOL_MOUSE) {
  353. koneplus = hid_get_drvdata(hdev);
  354. if (koneplus->roccat_claimed)
  355. roccat_disconnect(koneplus->chrdev_minor);
  356. kfree(koneplus);
  357. }
  358. }
  359. static int koneplus_probe(struct hid_device *hdev,
  360. const struct hid_device_id *id)
  361. {
  362. int retval;
  363. if (!hid_is_usb(hdev))
  364. return -EINVAL;
  365. retval = hid_parse(hdev);
  366. if (retval) {
  367. hid_err(hdev, "parse failed\n");
  368. goto exit;
  369. }
  370. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  371. if (retval) {
  372. hid_err(hdev, "hw start failed\n");
  373. goto exit;
  374. }
  375. retval = koneplus_init_specials(hdev);
  376. if (retval) {
  377. hid_err(hdev, "couldn't install mouse\n");
  378. goto exit_stop;
  379. }
  380. return 0;
  381. exit_stop:
  382. hid_hw_stop(hdev);
  383. exit:
  384. return retval;
  385. }
  386. static void koneplus_remove(struct hid_device *hdev)
  387. {
  388. koneplus_remove_specials(hdev);
  389. hid_hw_stop(hdev);
  390. }
  391. static void koneplus_keep_values_up_to_date(struct koneplus_device *koneplus,
  392. u8 const *data)
  393. {
  394. struct koneplus_mouse_report_button const *button_report;
  395. switch (data[0]) {
  396. case KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON:
  397. button_report = (struct koneplus_mouse_report_button const *)data;
  398. switch (button_report->type) {
  399. case KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_PROFILE:
  400. koneplus_profile_activated(koneplus, button_report->data1 - 1);
  401. break;
  402. }
  403. break;
  404. }
  405. }
  406. static void koneplus_report_to_chrdev(struct koneplus_device const *koneplus,
  407. u8 const *data)
  408. {
  409. struct koneplus_roccat_report roccat_report;
  410. struct koneplus_mouse_report_button const *button_report;
  411. if (data[0] != KONEPLUS_MOUSE_REPORT_NUMBER_BUTTON)
  412. return;
  413. button_report = (struct koneplus_mouse_report_button const *)data;
  414. if ((button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_QUICKLAUNCH ||
  415. button_report->type == KONEPLUS_MOUSE_REPORT_BUTTON_TYPE_TIMER) &&
  416. button_report->data2 != KONEPLUS_MOUSE_REPORT_BUTTON_ACTION_PRESS)
  417. return;
  418. roccat_report.type = button_report->type;
  419. roccat_report.data1 = button_report->data1;
  420. roccat_report.data2 = button_report->data2;
  421. roccat_report.profile = koneplus->actual_profile + 1;
  422. roccat_report_event(koneplus->chrdev_minor,
  423. (uint8_t const *)&roccat_report);
  424. }
  425. static int koneplus_raw_event(struct hid_device *hdev,
  426. struct hid_report *report, u8 *data, int size)
  427. {
  428. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  429. struct koneplus_device *koneplus = hid_get_drvdata(hdev);
  430. if (intf->cur_altsetting->desc.bInterfaceProtocol
  431. != USB_INTERFACE_PROTOCOL_MOUSE)
  432. return 0;
  433. if (koneplus == NULL)
  434. return 0;
  435. koneplus_keep_values_up_to_date(koneplus, data);
  436. if (koneplus->roccat_claimed)
  437. koneplus_report_to_chrdev(koneplus, data);
  438. return 0;
  439. }
  440. static const struct hid_device_id koneplus_devices[] = {
  441. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEPLUS) },
  442. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_KONEXTD) },
  443. { }
  444. };
  445. MODULE_DEVICE_TABLE(hid, koneplus_devices);
  446. static struct hid_driver koneplus_driver = {
  447. .name = "koneplus",
  448. .id_table = koneplus_devices,
  449. .probe = koneplus_probe,
  450. .remove = koneplus_remove,
  451. .raw_event = koneplus_raw_event
  452. };
  453. static int __init koneplus_init(void)
  454. {
  455. int retval;
  456. /* class name has to be same as driver name */
  457. retval = class_register(&koneplus_class);
  458. if (retval)
  459. return retval;
  460. retval = hid_register_driver(&koneplus_driver);
  461. if (retval)
  462. class_unregister(&koneplus_class);
  463. return retval;
  464. }
  465. static void __exit koneplus_exit(void)
  466. {
  467. hid_unregister_driver(&koneplus_driver);
  468. class_unregister(&koneplus_class);
  469. }
  470. module_init(koneplus_init);
  471. module_exit(koneplus_exit);
  472. MODULE_AUTHOR("Stefan Achatz");
  473. MODULE_DESCRIPTION("USB Roccat Kone[+]/XTD driver");
  474. MODULE_LICENSE("GPL v2");