hid-roccat-koneplus.c 16 KB

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