hid-roccat-isku.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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 = strict_strtoul(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 struct device_attribute isku_attributes[] = {
  92. __ATTR(actual_profile, 0660,
  93. isku_sysfs_show_actual_profile,
  94. isku_sysfs_set_actual_profile),
  95. __ATTR_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 =
  102. container_of(kobj, struct device, kobj)->parent->parent;
  103. struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
  104. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  105. int retval;
  106. if (off >= real_size)
  107. return 0;
  108. if (off != 0 || count != real_size)
  109. return -EINVAL;
  110. mutex_lock(&isku->isku_lock);
  111. retval = isku_receive(usb_dev, command, buf, real_size);
  112. mutex_unlock(&isku->isku_lock);
  113. return retval ? retval : real_size;
  114. }
  115. static ssize_t isku_sysfs_write(struct file *fp, struct kobject *kobj,
  116. void const *buf, loff_t off, size_t count,
  117. size_t real_size, uint command)
  118. {
  119. struct device *dev =
  120. container_of(kobj, struct device, kobj)->parent->parent;
  121. struct isku_device *isku = hid_get_drvdata(dev_get_drvdata(dev));
  122. struct usb_device *usb_dev = interface_to_usbdev(to_usb_interface(dev));
  123. int retval;
  124. if (off != 0 || count != real_size)
  125. return -EINVAL;
  126. mutex_lock(&isku->isku_lock);
  127. retval = roccat_common2_send_with_status(usb_dev, command,
  128. (void *)buf, real_size);
  129. mutex_unlock(&isku->isku_lock);
  130. return retval ? retval : real_size;
  131. }
  132. #define ISKU_SYSFS_W(thingy, THINGY) \
  133. static ssize_t isku_sysfs_write_ ## thingy(struct file *fp, struct kobject *kobj, \
  134. struct bin_attribute *attr, char *buf, \
  135. loff_t off, size_t count) \
  136. { \
  137. return isku_sysfs_write(fp, kobj, buf, off, count, \
  138. ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
  139. }
  140. #define ISKU_SYSFS_R(thingy, THINGY) \
  141. static ssize_t isku_sysfs_read_ ## thingy(struct file *fp, struct kobject *kobj, \
  142. struct bin_attribute *attr, char *buf, \
  143. loff_t off, size_t count) \
  144. { \
  145. return isku_sysfs_read(fp, kobj, buf, off, count, \
  146. ISKU_SIZE_ ## THINGY, ISKU_COMMAND_ ## THINGY); \
  147. }
  148. #define ISKU_SYSFS_RW(thingy, THINGY) \
  149. ISKU_SYSFS_R(thingy, THINGY) \
  150. ISKU_SYSFS_W(thingy, THINGY)
  151. #define ISKU_BIN_ATTR_RW(thingy, THINGY) \
  152. { \
  153. .attr = { .name = #thingy, .mode = 0660 }, \
  154. .size = ISKU_SIZE_ ## THINGY, \
  155. .read = isku_sysfs_read_ ## thingy, \
  156. .write = isku_sysfs_write_ ## thingy \
  157. }
  158. #define ISKU_BIN_ATTR_R(thingy, THINGY) \
  159. { \
  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. { \
  166. .attr = { .name = #thingy, .mode = 0220 }, \
  167. .size = ISKU_SIZE_ ## THINGY, \
  168. .write = isku_sysfs_write_ ## thingy \
  169. }
  170. ISKU_SYSFS_RW(macro, MACRO)
  171. ISKU_SYSFS_RW(keys_function, KEYS_FUNCTION)
  172. ISKU_SYSFS_RW(keys_easyzone, KEYS_EASYZONE)
  173. ISKU_SYSFS_RW(keys_media, KEYS_MEDIA)
  174. ISKU_SYSFS_RW(keys_thumbster, KEYS_THUMBSTER)
  175. ISKU_SYSFS_RW(keys_macro, KEYS_MACRO)
  176. ISKU_SYSFS_RW(keys_capslock, KEYS_CAPSLOCK)
  177. ISKU_SYSFS_RW(light, LIGHT)
  178. ISKU_SYSFS_RW(key_mask, KEY_MASK)
  179. ISKU_SYSFS_RW(last_set, LAST_SET)
  180. ISKU_SYSFS_W(talk, TALK)
  181. ISKU_SYSFS_R(info, INFO)
  182. ISKU_SYSFS_W(control, CONTROL)
  183. ISKU_SYSFS_W(reset, RESET)
  184. static struct bin_attribute isku_bin_attributes[] = {
  185. ISKU_BIN_ATTR_RW(macro, MACRO),
  186. ISKU_BIN_ATTR_RW(keys_function, KEYS_FUNCTION),
  187. ISKU_BIN_ATTR_RW(keys_easyzone, KEYS_EASYZONE),
  188. ISKU_BIN_ATTR_RW(keys_media, KEYS_MEDIA),
  189. ISKU_BIN_ATTR_RW(keys_thumbster, KEYS_THUMBSTER),
  190. ISKU_BIN_ATTR_RW(keys_macro, KEYS_MACRO),
  191. ISKU_BIN_ATTR_RW(keys_capslock, KEYS_CAPSLOCK),
  192. ISKU_BIN_ATTR_RW(light, LIGHT),
  193. ISKU_BIN_ATTR_RW(key_mask, KEY_MASK),
  194. ISKU_BIN_ATTR_RW(last_set, LAST_SET),
  195. ISKU_BIN_ATTR_W(talk, TALK),
  196. ISKU_BIN_ATTR_R(info, INFO),
  197. ISKU_BIN_ATTR_W(control, CONTROL),
  198. ISKU_BIN_ATTR_W(reset, RESET),
  199. __ATTR_NULL
  200. };
  201. static int isku_init_isku_device_struct(struct usb_device *usb_dev,
  202. struct isku_device *isku)
  203. {
  204. int retval;
  205. mutex_init(&isku->isku_lock);
  206. retval = isku_get_actual_profile(usb_dev);
  207. if (retval < 0)
  208. return retval;
  209. isku_profile_activated(isku, retval);
  210. return 0;
  211. }
  212. static int isku_init_specials(struct hid_device *hdev)
  213. {
  214. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  215. struct usb_device *usb_dev = interface_to_usbdev(intf);
  216. struct isku_device *isku;
  217. int retval;
  218. if (intf->cur_altsetting->desc.bInterfaceProtocol
  219. != ISKU_USB_INTERFACE_PROTOCOL) {
  220. hid_set_drvdata(hdev, NULL);
  221. return 0;
  222. }
  223. isku = kzalloc(sizeof(*isku), GFP_KERNEL);
  224. if (!isku) {
  225. hid_err(hdev, "can't alloc device descriptor\n");
  226. return -ENOMEM;
  227. }
  228. hid_set_drvdata(hdev, isku);
  229. retval = isku_init_isku_device_struct(usb_dev, isku);
  230. if (retval) {
  231. hid_err(hdev, "couldn't init struct isku_device\n");
  232. goto exit_free;
  233. }
  234. retval = roccat_connect(isku_class, hdev,
  235. sizeof(struct isku_roccat_report));
  236. if (retval < 0) {
  237. hid_err(hdev, "couldn't init char dev\n");
  238. } else {
  239. isku->chrdev_minor = retval;
  240. isku->roccat_claimed = 1;
  241. }
  242. return 0;
  243. exit_free:
  244. kfree(isku);
  245. return retval;
  246. }
  247. static void isku_remove_specials(struct hid_device *hdev)
  248. {
  249. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  250. struct isku_device *isku;
  251. if (intf->cur_altsetting->desc.bInterfaceProtocol
  252. != ISKU_USB_INTERFACE_PROTOCOL)
  253. return;
  254. isku = hid_get_drvdata(hdev);
  255. if (isku->roccat_claimed)
  256. roccat_disconnect(isku->chrdev_minor);
  257. kfree(isku);
  258. }
  259. static int isku_probe(struct hid_device *hdev,
  260. const struct hid_device_id *id)
  261. {
  262. int retval;
  263. retval = hid_parse(hdev);
  264. if (retval) {
  265. hid_err(hdev, "parse failed\n");
  266. goto exit;
  267. }
  268. retval = hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  269. if (retval) {
  270. hid_err(hdev, "hw start failed\n");
  271. goto exit;
  272. }
  273. retval = isku_init_specials(hdev);
  274. if (retval) {
  275. hid_err(hdev, "couldn't install keyboard\n");
  276. goto exit_stop;
  277. }
  278. return 0;
  279. exit_stop:
  280. hid_hw_stop(hdev);
  281. exit:
  282. return retval;
  283. }
  284. static void isku_remove(struct hid_device *hdev)
  285. {
  286. isku_remove_specials(hdev);
  287. hid_hw_stop(hdev);
  288. }
  289. static void isku_keep_values_up_to_date(struct isku_device *isku,
  290. u8 const *data)
  291. {
  292. struct isku_report_button const *button_report;
  293. switch (data[0]) {
  294. case ISKU_REPORT_NUMBER_BUTTON:
  295. button_report = (struct isku_report_button const *)data;
  296. switch (button_report->event) {
  297. case ISKU_REPORT_BUTTON_EVENT_PROFILE:
  298. isku_profile_activated(isku, button_report->data1 - 1);
  299. break;
  300. }
  301. break;
  302. }
  303. }
  304. static void isku_report_to_chrdev(struct isku_device const *isku,
  305. u8 const *data)
  306. {
  307. struct isku_roccat_report roccat_report;
  308. struct isku_report_button const *button_report;
  309. if (data[0] != ISKU_REPORT_NUMBER_BUTTON)
  310. return;
  311. button_report = (struct isku_report_button const *)data;
  312. roccat_report.event = button_report->event;
  313. roccat_report.data1 = button_report->data1;
  314. roccat_report.data2 = button_report->data2;
  315. roccat_report.profile = isku->actual_profile + 1;
  316. roccat_report_event(isku->chrdev_minor,
  317. (uint8_t const *)&roccat_report);
  318. }
  319. static int isku_raw_event(struct hid_device *hdev,
  320. struct hid_report *report, u8 *data, int size)
  321. {
  322. struct usb_interface *intf = to_usb_interface(hdev->dev.parent);
  323. struct isku_device *isku = hid_get_drvdata(hdev);
  324. if (intf->cur_altsetting->desc.bInterfaceProtocol
  325. != ISKU_USB_INTERFACE_PROTOCOL)
  326. return 0;
  327. if (isku == NULL)
  328. return 0;
  329. isku_keep_values_up_to_date(isku, data);
  330. if (isku->roccat_claimed)
  331. isku_report_to_chrdev(isku, data);
  332. return 0;
  333. }
  334. static const struct hid_device_id isku_devices[] = {
  335. { HID_USB_DEVICE(USB_VENDOR_ID_ROCCAT, USB_DEVICE_ID_ROCCAT_ISKU) },
  336. { }
  337. };
  338. MODULE_DEVICE_TABLE(hid, isku_devices);
  339. static struct hid_driver isku_driver = {
  340. .name = "isku",
  341. .id_table = isku_devices,
  342. .probe = isku_probe,
  343. .remove = isku_remove,
  344. .raw_event = isku_raw_event
  345. };
  346. static int __init isku_init(void)
  347. {
  348. int retval;
  349. isku_class = class_create(THIS_MODULE, "isku");
  350. if (IS_ERR(isku_class))
  351. return PTR_ERR(isku_class);
  352. isku_class->dev_attrs = isku_attributes;
  353. isku_class->dev_bin_attrs = isku_bin_attributes;
  354. retval = hid_register_driver(&isku_driver);
  355. if (retval)
  356. class_destroy(isku_class);
  357. return retval;
  358. }
  359. static void __exit isku_exit(void)
  360. {
  361. hid_unregister_driver(&isku_driver);
  362. class_destroy(isku_class);
  363. }
  364. module_init(isku_init);
  365. module_exit(isku_exit);
  366. MODULE_AUTHOR("Stefan Achatz");
  367. MODULE_DESCRIPTION("USB Roccat Isku driver");
  368. MODULE_LICENSE("GPL v2");