hid-roccat-koneplus.c 16 KB

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