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 "hid-roccat-common.h"
  15. static inline uint16_t roccat_common_feature_report(uint8_t report_id)
  16. {
  17. return 0x300 | report_id;
  18. }
  19. int roccat_common_receive(struct usb_device *usb_dev, uint report_id,
  20. void *data, uint size)
  21. {
  22. char *buf;
  23. int len;
  24. buf = kmalloc(size, GFP_KERNEL);
  25. if (buf == NULL)
  26. return -ENOMEM;
  27. len = usb_control_msg(usb_dev, usb_rcvctrlpipe(usb_dev, 0),
  28. HID_REQ_GET_REPORT,
  29. USB_TYPE_CLASS | USB_RECIP_INTERFACE | USB_DIR_IN,
  30. roccat_common_feature_report(report_id),
  31. 0, buf, size, USB_CTRL_SET_TIMEOUT);
  32. memcpy(data, buf, size);
  33. kfree(buf);
  34. return ((len < 0) ? len : ((len != size) ? -EIO : 0));
  35. }
  36. EXPORT_SYMBOL_GPL(roccat_common_receive);
  37. int roccat_common_send(struct usb_device *usb_dev, uint report_id,
  38. void const *data, uint size)
  39. {
  40. char *buf;
  41. int len;
  42. buf = kmalloc(size, GFP_KERNEL);
  43. if (buf == NULL)
  44. return -ENOMEM;
  45. memcpy(buf, data, size);
  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");