hid-roccat-isku.c 11 KB

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