amd_sfh_hid.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * AMD MP2 Sensors transport driver
  4. *
  5. * Copyright 2020-2021 Advanced Micro Devices, Inc.
  6. * Authors: Nehal Bakulchandra Shah <[email protected]>
  7. * Sandeep Singh <[email protected]>
  8. * Basavaraj Natikar <[email protected]>
  9. */
  10. #include <linux/hid.h>
  11. #include <linux/wait.h>
  12. #include <linux/sched.h>
  13. #include "amd_sfh_hid.h"
  14. #include "amd_sfh_pcie.h"
  15. #define AMD_SFH_RESPONSE_TIMEOUT 1500
  16. /**
  17. * amdtp_hid_parse() - hid-core .parse() callback
  18. * @hid: hid device instance
  19. *
  20. * This function gets called during call to hid_add_device
  21. *
  22. * Return: 0 on success and non zero on error
  23. */
  24. static int amdtp_hid_parse(struct hid_device *hid)
  25. {
  26. struct amdtp_hid_data *hid_data = hid->driver_data;
  27. struct amdtp_cl_data *cli_data = hid_data->cli_data;
  28. return hid_parse_report(hid, cli_data->report_descr[hid_data->index],
  29. cli_data->report_descr_sz[hid_data->index]);
  30. }
  31. /* Empty callbacks with success return code */
  32. static int amdtp_hid_start(struct hid_device *hid)
  33. {
  34. return 0;
  35. }
  36. static void amdtp_hid_stop(struct hid_device *hid)
  37. {
  38. }
  39. static int amdtp_hid_open(struct hid_device *hid)
  40. {
  41. return 0;
  42. }
  43. static void amdtp_hid_close(struct hid_device *hid)
  44. {
  45. }
  46. static int amdtp_raw_request(struct hid_device *hdev, u8 reportnum,
  47. u8 *buf, size_t len, u8 rtype, int reqtype)
  48. {
  49. return 0;
  50. }
  51. static void amdtp_hid_request(struct hid_device *hid, struct hid_report *rep, int reqtype)
  52. {
  53. int rc;
  54. switch (reqtype) {
  55. case HID_REQ_GET_REPORT:
  56. rc = amd_sfh_get_report(hid, rep->id, rep->type);
  57. if (rc)
  58. dev_err(&hid->dev, "AMDSFH get report error\n");
  59. break;
  60. case HID_REQ_SET_REPORT:
  61. amd_sfh_set_report(hid, rep->id, reqtype);
  62. break;
  63. default:
  64. break;
  65. }
  66. }
  67. static int amdtp_wait_for_response(struct hid_device *hid)
  68. {
  69. struct amdtp_hid_data *hid_data = hid->driver_data;
  70. struct amdtp_cl_data *cli_data = hid_data->cli_data;
  71. int i, ret = 0;
  72. for (i = 0; i < cli_data->num_hid_devices; i++) {
  73. if (cli_data->hid_sensor_hubs[i] == hid)
  74. break;
  75. }
  76. if (!cli_data->request_done[i])
  77. ret = wait_event_interruptible_timeout(hid_data->hid_wait,
  78. cli_data->request_done[i],
  79. msecs_to_jiffies(AMD_SFH_RESPONSE_TIMEOUT));
  80. if (ret == -ERESTARTSYS)
  81. return -ERESTARTSYS;
  82. else if (ret < 0)
  83. return -ETIMEDOUT;
  84. else
  85. return 0;
  86. }
  87. void amdtp_hid_wakeup(struct hid_device *hid)
  88. {
  89. struct amdtp_hid_data *hid_data;
  90. struct amdtp_cl_data *cli_data;
  91. if (hid) {
  92. hid_data = hid->driver_data;
  93. cli_data = hid_data->cli_data;
  94. cli_data->request_done[cli_data->cur_hid_dev] = true;
  95. wake_up_interruptible(&hid_data->hid_wait);
  96. }
  97. }
  98. static struct hid_ll_driver amdtp_hid_ll_driver = {
  99. .parse = amdtp_hid_parse,
  100. .start = amdtp_hid_start,
  101. .stop = amdtp_hid_stop,
  102. .open = amdtp_hid_open,
  103. .close = amdtp_hid_close,
  104. .request = amdtp_hid_request,
  105. .wait = amdtp_wait_for_response,
  106. .raw_request = amdtp_raw_request,
  107. };
  108. int amdtp_hid_probe(u32 cur_hid_dev, struct amdtp_cl_data *cli_data)
  109. {
  110. struct amd_mp2_dev *mp2 = container_of(cli_data->in_data, struct amd_mp2_dev, in_data);
  111. struct device *dev = &mp2->pdev->dev;
  112. struct hid_device *hid;
  113. struct amdtp_hid_data *hid_data;
  114. int rc;
  115. hid = hid_allocate_device();
  116. if (IS_ERR(hid))
  117. return PTR_ERR(hid);
  118. hid_data = kzalloc(sizeof(*hid_data), GFP_KERNEL);
  119. if (!hid_data) {
  120. rc = -ENOMEM;
  121. goto err_hid_data;
  122. }
  123. hid->ll_driver = &amdtp_hid_ll_driver;
  124. hid_data->index = cur_hid_dev;
  125. hid_data->cli_data = cli_data;
  126. init_waitqueue_head(&hid_data->hid_wait);
  127. hid->driver_data = hid_data;
  128. cli_data->hid_sensor_hubs[cur_hid_dev] = hid;
  129. strscpy(hid->phys, dev->driver ? dev->driver->name : dev_name(dev),
  130. sizeof(hid->phys));
  131. hid->bus = BUS_AMD_SFH;
  132. hid->vendor = AMD_SFH_HID_VENDOR;
  133. hid->product = AMD_SFH_HID_PRODUCT;
  134. snprintf(hid->name, sizeof(hid->name), "%s %04X:%04X", "hid-amdsfh",
  135. hid->vendor, hid->product);
  136. rc = hid_add_device(hid);
  137. if (rc)
  138. goto err_hid_device;
  139. return 0;
  140. err_hid_device:
  141. kfree(hid_data);
  142. err_hid_data:
  143. hid_destroy_device(hid);
  144. return rc;
  145. }
  146. void amdtp_hid_remove(struct amdtp_cl_data *cli_data)
  147. {
  148. int i;
  149. for (i = 0; i < cli_data->num_hid_devices; ++i) {
  150. if (cli_data->hid_sensor_hubs[i]) {
  151. kfree(cli_data->hid_sensor_hubs[i]->driver_data);
  152. hid_destroy_device(cli_data->hid_sensor_hubs[i]);
  153. cli_data->hid_sensor_hubs[i] = NULL;
  154. }
  155. }
  156. }