hid-roccat-common.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Roccat common functions for device specific drivers
  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. #include <linux/hid.h>
  13. #include <linux/slab.h>
  14. #include <linux/module.h>
  15. #include "hid-roccat-common.h"
  16. static inline uint16_t roccat_common2_feature_report(uint8_t report_id)
  17. {
  18. return 0x300 | report_id;
  19. }
  20. int roccat_common2_receive(struct usb_device *usb_dev, uint report_id,
  21. void *data, uint size)
  22. {
  23. char *buf;
  24. int len;
  25. buf = kmalloc(size, GFP_KERNEL);
  26. if (buf == NULL)
  27. return -ENOMEM;
  28. len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  29. HID_REQ_GET_REPORT,
  30. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  31. roccat_common2_feature_report(report_id),
  32. 0, buf, size, USB_CTRL_SET_TIMEOUT);
  33. memcpy(data, buf, size);
  34. kfree(buf);
  35. return ((len < 0) ? len : ((len != size) ? -EIO : 0));
  36. }
  37. EXPORT_SYMBOL_GPL(roccat_common2_receive);
  38. int roccat_common2_send(struct usb_device *usb_dev, uint report_id,
  39. void const *data, uint size)
  40. {
  41. char *buf;
  42. int len;
  43. buf = kmemdup(data, size, GFP_KERNEL);
  44. if (buf == NULL)
  45. return -ENOMEM;
  46. len = usb_control_msg(usb_dev, usb_sndctrlpipe(usb_dev, 0),
  47. HID_REQ_SET_REPORT,
  48. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_OUT,
  49. roccat_common2_feature_report(report_id),
  50. 0, buf, size, USB_CTRL_SET_TIMEOUT);
  51. kfree(buf);
  52. return ((len < 0) ? len : ((len != size) ? -EIO : 0));
  53. }
  54. EXPORT_SYMBOL_GPL(roccat_common2_send);
  55. enum roccat_common2_control_states {
  56. ROCCAT_COMMON_CONTROL_STATUS_OVERLOAD = 0,
  57. ROCCAT_COMMON_CONTROL_STATUS_OK = 1,
  58. ROCCAT_COMMON_CONTROL_STATUS_INVALID = 2,
  59. ROCCAT_COMMON_CONTROL_STATUS_WAIT = 3,
  60. };
  61. static int roccat_common2_receive_control_status(struct usb_device *usb_dev)
  62. {
  63. int retval;
  64. struct roccat_common2_control control;
  65. do {
  66. msleep(50);
  67. retval = roccat_common2_receive(usb_dev,
  68. ROCCAT_COMMON_COMMAND_CONTROL,
  69. &control, sizeof(struct roccat_common2_control));
  70. if (retval)
  71. return retval;
  72. switch (control.value) {
  73. case ROCCAT_COMMON_CONTROL_STATUS_OK:
  74. return 0;
  75. case ROCCAT_COMMON_CONTROL_STATUS_WAIT:
  76. msleep(500);
  77. continue;
  78. case ROCCAT_COMMON_CONTROL_STATUS_INVALID:
  79. case ROCCAT_COMMON_CONTROL_STATUS_OVERLOAD:
  80. /* seems to be critical - replug necessary */
  81. return -EINVAL;
  82. default:
  83. dev_err(&usb_dev->dev,
  84. "roccat_common2_receive_control_status: "
  85. "unknown response value 0x%x\n",
  86. control.value);
  87. return -EINVAL;
  88. }
  89. } while (1);
  90. }
  91. int roccat_common2_send_with_status(struct usb_device *usb_dev,
  92. uint command, void const *buf, uint size)
  93. {
  94. int retval;
  95. retval = roccat_common2_send(usb_dev, command, buf, size);
  96. if (retval)
  97. return retval;
  98. msleep(100);
  99. return roccat_common2_receive_control_status(usb_dev);
  100. }
  101. EXPORT_SYMBOL_GPL(roccat_common2_send_with_status);
  102. MODULE_AUTHOR("Stefan Achatz");
  103. MODULE_DESCRIPTION("USB Roccat common driver");
  104. MODULE_LICENSE("GPL v2");