hid-roccat-koneplus.c 15 KB

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