sdca_hid.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
  2. /*
  3. * The MIPI SDCA specification is available for public downloads at
  4. * https://www.mipi.org/mipi-sdca-v1-0-download
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/byteorder/generic.h>
  8. #include <linux/cleanup.h>
  9. #include <linux/device.h>
  10. #include <linux/dev_printk.h>
  11. #include <linux/hid.h>
  12. #include <linux/module.h>
  13. #include <linux/property.h>
  14. #include <linux/soundwire/sdw.h>
  15. #include <linux/types.h>
  16. #include <sound/sdca.h>
  17. #include <sound/sdca_function.h>
  18. #include <sound/sdca_hid.h>
  19. #include <sound/sdca_interrupts.h>
  20. #include <sound/sdca_ump.h>
  21. static int sdwhid_parse(struct hid_device *hid)
  22. {
  23. struct sdca_entity *entity = hid->driver_data;
  24. unsigned int rsize;
  25. int ret;
  26. rsize = le16_to_cpu(entity->hide.hid_desc.rpt_desc.wDescriptorLength);
  27. if (!rsize || rsize > HID_MAX_DESCRIPTOR_SIZE) {
  28. dev_err(&hid->dev, "invalid size of report descriptor (%u)\n", rsize);
  29. return -EINVAL;
  30. }
  31. ret = hid_parse_report(hid, entity->hide.hid_report_desc, rsize);
  32. if (!ret)
  33. return 0;
  34. dev_err(&hid->dev, "parsing report descriptor failed\n");
  35. return ret;
  36. }
  37. static int sdwhid_start(struct hid_device *hid)
  38. {
  39. return 0;
  40. }
  41. static void sdwhid_stop(struct hid_device *hid)
  42. {
  43. }
  44. static int sdwhid_raw_request(struct hid_device *hid, unsigned char reportnum,
  45. __u8 *buf, size_t len, unsigned char rtype, int reqtype)
  46. {
  47. switch (reqtype) {
  48. case HID_REQ_GET_REPORT:
  49. /* not implemented yet */
  50. return 0;
  51. case HID_REQ_SET_REPORT:
  52. /* not implemented yet */
  53. return 0;
  54. default:
  55. return -EIO;
  56. }
  57. }
  58. static int sdwhid_open(struct hid_device *hid)
  59. {
  60. return 0;
  61. }
  62. static void sdwhid_close(struct hid_device *hid)
  63. {
  64. }
  65. static const struct hid_ll_driver sdw_hid_driver = {
  66. .parse = sdwhid_parse,
  67. .start = sdwhid_start,
  68. .stop = sdwhid_stop,
  69. .open = sdwhid_open,
  70. .close = sdwhid_close,
  71. .raw_request = sdwhid_raw_request,
  72. };
  73. int sdca_add_hid_device(struct device *dev, struct sdw_slave *sdw,
  74. struct sdca_entity *entity)
  75. {
  76. struct sdw_bus *bus = sdw->bus;
  77. struct hid_device *hid;
  78. int ret;
  79. hid = hid_allocate_device();
  80. if (IS_ERR(hid))
  81. return PTR_ERR(hid);
  82. hid->ll_driver = &sdw_hid_driver;
  83. hid->dev.parent = dev;
  84. hid->bus = BUS_SDW;
  85. hid->version = le16_to_cpu(entity->hide.hid_desc.bcdHID);
  86. snprintf(hid->name, sizeof(hid->name),
  87. "HID sdw:%01x:%01x:%04x:%04x:%02x",
  88. bus->controller_id, bus->link_id, sdw->id.mfg_id,
  89. sdw->id.part_id, sdw->id.class_id);
  90. snprintf(hid->phys, sizeof(hid->phys), "%s", dev->bus->name);
  91. hid->driver_data = entity;
  92. ret = hid_add_device(hid);
  93. if (ret && ret != -ENODEV) {
  94. dev_err(dev, "can't add hid device: %d\n", ret);
  95. hid_destroy_device(hid);
  96. return ret;
  97. }
  98. entity->hide.hid = hid;
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_NS(sdca_add_hid_device, "SND_SOC_SDCA");
  102. /**
  103. * sdca_hid_process_report - read a HID event from the device and report
  104. * @interrupt: Pointer to the SDCA interrupt information structure.
  105. *
  106. * Return: Zero on success, and a negative error code on failure.
  107. */
  108. int sdca_hid_process_report(struct sdca_interrupt *interrupt)
  109. {
  110. struct device *dev = interrupt->dev;
  111. struct hid_device *hid = interrupt->entity->hide.hid;
  112. void *val __free(kfree) = NULL;
  113. int len, ret;
  114. ret = sdca_ump_get_owner_host(dev, interrupt->function_regmap,
  115. interrupt->function, interrupt->entity,
  116. interrupt->control);
  117. if (ret)
  118. return ret;
  119. len = sdca_ump_read_message(dev, interrupt->device_regmap,
  120. interrupt->function_regmap,
  121. interrupt->function, interrupt->entity,
  122. SDCA_CTL_HIDE_HIDTX_MESSAGEOFFSET,
  123. SDCA_CTL_HIDE_HIDTX_MESSAGELENGTH, &val);
  124. if (len < 0)
  125. return len;
  126. ret = sdca_ump_set_owner_device(dev, interrupt->function_regmap,
  127. interrupt->function, interrupt->entity,
  128. interrupt->control);
  129. if (ret)
  130. return ret;
  131. ret = hid_input_report(hid, HID_INPUT_REPORT, val, len, true);
  132. if (ret < 0) {
  133. dev_err(dev, "failed to report hid event: %d\n", ret);
  134. return ret;
  135. }
  136. return 0;
  137. }
  138. EXPORT_SYMBOL_NS(sdca_hid_process_report, "SND_SOC_SDCA");