hid-roccat-isku.c 12 KB

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