hid-roccat-common.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_common_feature_report(uint8_t report_id)
  17. {
  18. return 0x300 | report_id;
  19. }
  20. int roccat_common_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_common_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_common_receive);
  38. int roccat_common_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_common_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_common_send);
  55. MODULE_AUTHOR("Stefan Achatz");
  56. MODULE_DESCRIPTION("USB Roccat common driver");
  57. MODULE_LICENSE("GPL v2");