hid-roccat-isku.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Roccat Isku driver for Linux
  3. *
  4. * Copyright (c) 2011 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 Isku is a gamer keyboard with macro keys that can be configured in
  14. * 5 profiles.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/input.h>
  18. #include <linux/hid.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/hid-roccat.h>
  22. #include "hid-ids.h"
  23. #include "hid-roccat-common.h"
  24. #include "hid-roccat-isku.h"
  25. static struct class *isku_class;
  26. static void isku_profile_activated(struct isku_device *isku, uint new_profile)
  27. {
  28. isku->actual_profile = new_profile;
  29. }
  30. static int isku_receive(struct usb_device *usb_dev, uint command,
  31. void *buf, uint size)
  32. {
  33. return roccat_common2_receive(usb_dev, command, buf, size);
  34. }
  35. static int isku_get_actual_profile(struct usb_device *usb_dev)
  36. {
  37. struct isku_actual_profile buf;
  38. int retval;
  39. retval = isku_receive(usb_dev, ISKU_COMMAND_ACTUAL_PROFILE,
  40. &buf, sizeof(struct isku_actual_profile));
  41. return retval ? retval : buf.actual_profile;
  42. }
  43. static int isku_set_actual_profile(struct usb_device *usb_dev, int new_profile)
  44. {
  45. struct isku_actual_profile buf;
  46. buf.command = ISKU_COMMAND_ACTUAL_PROFILE;
  47. buf.size = sizeof(struct isku_actual_profile);
  48. buf.actual_profile = new_profile;
  49. return roccat_common2_send_with_status(usb_dev,
  50. ISKU_COMMAND_ACTUAL_PROFILE, &buf,
  51. sizeof(struct isku_actual_profile));
  52. }
  53. static ssize_t isku_sysfs_show_actual_profile(struct device *dev,
  54. struct device_attribute *attr, char *buf)
  55. {
  56. struct isku_device *isku =
  57. hid_get_drvdata(dev_get_drvdata(dev->parent->parent));
  58. return snprintf(buf, PAGE_SIZE, "%d\n", isku->actual_profile);
  59. }
  60. static ssize_t isku_sysfs_set_actual_profile(struct device *dev,
  61. struct device_attribute *attr, char const *buf, size_t size)
  62. {
  63. struct isku_device *isku;
  64. struct usb_device *usb_dev;
  65. unsigned long profile;
  66. int retval;
  67. struct isku_roccat_report roccat_report;
  68. dev = dev->parent->parent;
  69. isku = hid_get_drvdata(dev_get_drvdata(dev));
  70. usb_dev = interface_to_usbdev(to_usb_interface(dev));
  71. retval = kstrtoul(buf, 10, &profile);
  72. if (retval)
  73. return retval;
  74. if (profile > 4)
  75. return -EINVAL;
  76. mutex_lock(&isku->isku_lock);
  77. retval = isku_set_actual_profile(usb_dev, profile);
  78. if (retval) {
  79. mutex_unlock(&isku->isku_lock);
  80. return retval;
  81. }
  82. isku_profile_activated(isku, profile);
  83. roccat_report.event = ISKU_REPORT_BUTTON_EVENT_PROFILE;
  84. roccat_report.data1 = profile + 1;
  85. roccat_report.data2 = 0;
  86. roccat_report.profile = profile + 1;
  87. roccat_report_event(isku->chrdev_minor, (uint8_t const *)&roccat_report);
  88. mutex_unlock(&isku->isku_lock);
  89. return size;
  90. }
  91. static DEVICE_ATTR(actual_profile, 0660, isku_sysfs_show_actual_profile,
  92. isku_sysfs_set_actual_profile);
  93. static struct attribute *isku_attrs[] = {
  94. &dev_attr_actual_profile.attr,
  95. NULL,
  96. };
  97. static ssize_t isku_sysfs_read(struct file *fp, struct kobject *kobj,
  98. char *buf, loff_t off, size_t count,
  99. size_t real_size, uint command)
  100. {
  101. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  102. struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
  103. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  104. int retval;
  105. if (off >= real_size)
  106. return 0;
  107. if (off != 0 || count > real_size)
  108. return -EINVAL;
  109. mutex_lock(&isku->isku_lock);
  110. retval = isku_receive(usb_dev, command, buf, count);
  111. mutex_unlock(&isku->isku_lock);
  112. return retval ? retval : count;
  113. }
  114. static ssize_t isku_sysfs_write(struct file *fp, struct kobject *kobj,
  115. void const *buf, loff_t off, size_t count,
  116. size_t real_size, uint command)
  117. {
  118. struct device *dev = kobj_to_dev(kobj)->parent->parent;
  119. struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
  120. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  121. int retval;
  122. if (off != 0 || count > real_size)
  123. return -EINVAL;
  124. mutex_lock(&isku->isku_lock);
  125. retval = roccat_common2_send_with_status(usb_dev, command,
  126. (void *)buf, count);
  127. mutex_unlock(&isku->isku_lock);
  128. return retval ? retval : count;
  129. }
  130. #define ISKU_SYSFS_W(thingy, THINGY) \
  131. static ssize_t isku_sysfs_write_ ## thingy(struct file *fp, struct kobject *kobj, \
  132. struct bin_attribute *attr, char *buf, \
  133. loff_t off, size_t count) \
  134. { \
  135. return isku_sysfs_write(fp, kobj, buf, off, count, \
  136. ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
  137. }
  138. #define ISKU_SYSFS_R(thingy, THINGY) \
  139. static ssize_t isku_sysfs_read_ ## thingy(struct file *fp, struct kobject *kobj, \
  140. struct bin_attribute *attr, char *buf, \
  141. loff_t off, size_t count) \
  142. { \
  143. return isku_sysfs_read(fp, kobj, buf, off, count, \
  144. ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
  145. }
  146. #define ISKU_SYSFS_RW(thingy, THINGY) \
  147. ISKU_SYSFS_R(thingy, THINGY) \
  148. ISKU_SYSFS_W(thingy, THINGY)
  149. #define ISKU_BIN_ATTR_RW(thingy, THINGY) \
  150. ISKU_SYSFS_RW(thingy, THINGY); \
  151. static struct bin_attribute bin_attr_##thingy = { \
  152. .attr = { .name = #thingy, .mode = 0660 }, \
  153. .size = ISKU_SIZE_ ## THINGY, \
  154. .read = isku_sysfs_read_ ## thingy, \
  155. .write = isku_sysfs_write_ ## thingy \
  156. }
  157. #define ISKU_BIN_ATTR_R(thingy, THINGY) \
  158. ISKU_SYSFS_R(thingy, THINGY); \
  159. static struct bin_attribute bin_attr_##thingy = { \
  160. .attr = { .name = #thingy, .mode = 0440 }, \
  161. .size = ISKU_SIZE_ ## THINGY, \
  162. .read = isku_sysfs_read_ ## thingy, \
  163. }
  164. #define ISKU_BIN_ATTR_W(thingy, THINGY) \
  165. ISKU_SYSFS_W(thingy, THINGY); \
  166. static struct bin_attribute bin_attr_##thingy = { \
  167. .attr = { .name = #thingy, .mode = 0220 }, \
  168. .size = ISKU_SIZE_ ## THINGY, \
  169. .write = isku_sysfs_write_ ## thingy \
  170. }
  171. ISKU_BIN_ATTR_RW(macro, MACRO);
  172. ISKU_BIN_ATTR_RW(keys_function, KEYS_FUNCTION);
  173. ISKU_BIN_ATTR_RW(keys_easyzone, KEYS_EASYZONE);
  174. ISKU_BIN_ATTR_RW(keys_media, KEYS_MEDIA);
  175. ISKU_BIN_ATTR_RW(keys_thumbster, KEYS_THUMBSTER);
  176. ISKU_BIN_ATTR_RW(keys_macro, KEYS_MACRO);
  177. ISKU_BIN_ATTR_RW(keys_capslock, KEYS_CAPSLOCK);
  178. ISKU_BIN_ATTR_RW(light, LIGHT);
  179. ISKU_BIN_ATTR_RW(key_mask, KEY_MASK);
  180. ISKU_BIN_ATTR_RW(last_set, LAST_SET);
  181. ISKU_BIN_ATTR_W(talk, TALK);
  182. ISKU_BIN_ATTR_W(talkfx, TALKFX);
  183. ISKU_BIN_ATTR_W(control, CONTROL);
  184. ISKU_BIN_ATTR_W(reset, RESET);
  185. ISKU_BIN_ATTR_R(info, INFO);
  186. static struct bin_attribute *isku_bin_attributes[] = {
  187. &bin_attr_macro,
  188. &bin_attr_keys_function,
  189. &bin_attr_keys_easyzone,
  190. &bin_attr_keys_media,
  191. &bin_attr_keys_thumbster,
  192. &bin_attr_keys_macro,
  193. &bin_attr_keys_capslock,
  194. &bin_attr_light,
  195. &bin_attr_key_mask,
  196. &bin_attr_last_set,
  197. &bin_attr_talk,
  198. &bin_attr_talkfx,
  199. &bin_attr_control,
  200. &bin_attr_reset,
  201. &bin_attr_info,
  202. NULL,
  203. };
  204. static const struct attribute_group isku_group = {
  205. .attrs = isku_attrs,
  206. .bin_attrs = isku_bin_attributes,
  207. };
  208. static const struct attribute_group *isku_groups[] = {
  209. &isku_group,
  210. NULL,
  211. };
  212. static int isku_init_isku_device_struct(struct usb_device *usb_dev,
  213. struct isku_device *isku)
  214. {
  215. int retval;
  216. mutex_init(&isku->isku_lock);
  217. retval = isku_get_actual_profile(usb_dev);
  218. if (retval < 0)
  219. return retval;
  220. isku_profile_activated(isku, retval);
  221. return 0;
  222. }
  223. static int isku_init_specials(struct hid_device *hdev)
  224. {
  225. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  226. struct usb_device *usb_dev = interface_to_usbdev(intf);
  227. struct isku_device *isku;
  228. int retval;
  229. if (intf->cur_altsetting->desc.bInterfaceProtocol
  230. != ISKU_USB_INTERFACE_PROTOCOL) {
  231. hid_set_drvdata(hdev, NULL);
  232. return 0;
  233. }
  234. isku = kzalloc(sizeof(*isku), GFP_KERNEL);
  235. if (!isku) {
  236. hid_err(hdev, "can't alloc device descriptor\n");
  237. return -ENOMEM;
  238. }
  239. hid_set_drvdata(hdev, isku);
  240. retval = isku_init_isku_device_struct(usb_dev, isku);
  241. if (retval) {
  242. hid_err(hdev, "couldn't init struct isku_device\n");
  243. goto exit_free;
  244. }
  245. retval = roccat_connect(isku_class, hdev,
  246. sizeof(struct isku_roccat_report));
  247. if (retval < 0) {
  248. hid_err(hdev, "couldn't init char dev\n");
  249. } else {
  250. isku->chrdev_minor = retval;
  251. isku->roccat_claimed = 1;
  252. }
  253. return 0;
  254. exit_free:
  255. kfree(isku);
  256. return retval;
  257. }
  258. static void isku_remove_specials(struct hid_device *hdev)
  259. {
  260. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  261. struct isku_device *isku;
  262. if (intf->cur_altsetting->desc.bInterfaceProtocol
  263. != ISKU_USB_INTERFACE_PROTOCOL)
  264. return;
  265. isku = hid_get_drvdata(hdev);
  266. if (isku->roccat_claimed)
  267. roccat_disconnect(isku->chrdev_minor);
  268. kfree(isku);
  269. }
  270. static int isku_probe(struct hid_device *hdev,
  271. const struct hid_device_id *id)
  272. {
  273. int retval;
  274. retval = hid_parse(hdev);
  275. if (retval) {
  276. hid_err(hdev, "parse failed\n");
  277. goto exit;
  278. }
  279. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  280. if (retval) {
  281. hid_err(hdev, "hw start failed\n");
  282. goto exit;
  283. }
  284. retval = isku_init_specials(hdev);
  285. if (retval) {
  286. hid_err(hdev, "couldn't install keyboard\n");
  287. goto exit_stop;
  288. }
  289. return 0;
  290. exit_stop:
  291. hid_hw_stop(hdev);
  292. exit:
  293. return retval;
  294. }
  295. static void isku_remove(struct hid_device *hdev)
  296. {
  297. isku_remove_specials(hdev);
  298. hid_hw_stop(hdev);
  299. }
  300. static void isku_keep_values_up_to_date(struct isku_device *isku,
  301. u8 const *data)
  302. {
  303. struct isku_report_button const *button_report;
  304. switch (data[0]) {
  305. case ISKU_REPORT_NUMBER_BUTTON:
  306. button_report = (struct isku_report_button const *)data;
  307. switch (button_report->event) {
  308. case ISKU_REPORT_BUTTON_EVENT_PROFILE:
  309. isku_profile_activated(isku, button_report->data1 - 1);
  310. break;
  311. }
  312. break;
  313. }
  314. }
  315. static void isku_report_to_chrdev(struct isku_device const *isku,
  316. u8 const *data)
  317. {
  318. struct isku_roccat_report roccat_report;
  319. struct isku_report_button const *button_report;
  320. if (data[0] != ISKU_REPORT_NUMBER_BUTTON)
  321. return;
  322. button_report = (struct isku_report_button const *)data;
  323. roccat_report.event = button_report->event;
  324. roccat_report.data1 = button_report->data1;
  325. roccat_report.data2 = button_report->data2;
  326. roccat_report.profile = isku->actual_profile + 1;
  327. roccat_report_event(isku->chrdev_minor,
  328. (uint8_t const *)&roccat_report);
  329. }
  330. static int isku_raw_event(struct hid_device *hdev,
  331. struct hid_report *report, u8 *data, int size)
  332. {
  333. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  334. struct isku_device *isku = hid_get_drvdata(hdev);
  335. if (intf->cur_altsetting->desc.bInterfaceProtocol
  336. != ISKU_USB_INTERFACE_PROTOCOL)
  337. return 0;
  338. if (isku == NULL)
  339. return 0;
  340. isku_keep_values_up_to_date(isku, data);
  341. if (isku->roccat_claimed)
  342. isku_report_to_chrdev(isku, data);
  343. return 0;
  344. }
  345. static const struct hid_device_id isku_devices[] = {
  346. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
  347. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKUFX) },
  348. { }
  349. };
  350. MODULE_DEVICE_TABLE(hid, isku_devices);
  351. static struct hid_driver isku_driver = {
  352. .name = "isku",
  353. .id_table = isku_devices,
  354. .probe = isku_probe,
  355. .remove = isku_remove,
  356. .raw_event = isku_raw_event
  357. };
  358. static int __init isku_init(void)
  359. {
  360. int retval;
  361. isku_class = class_create(THIS_MODULE, "isku");
  362. if (IS_ERR(isku_class))
  363. return PTR_ERR(isku_class);
  364. isku_class->dev_groups = isku_groups;
  365. retval = hid_register_driver(&isku_driver);
  366. if (retval)
  367. class_destroy(isku_class);
  368. return retval;
  369. }
  370. static void __exit isku_exit(void)
  371. {
  372. hid_unregister_driver(&isku_driver);
  373. class_destroy(isku_class);
  374. }
  375. module_init(isku_init);
  376. module_exit(isku_exit);
  377. MODULE_AUTHOR("Stefan Achatz");
  378. MODULE_DESCRIPTION("USB Roccat Isku/FX driver");
  379. MODULE_LICENSE("GPL v2");