audio_pdr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/module.h>
  6. #include <linux/slab.h>
  7. #include <soc/qcom/service-locator.h>
  8. #include <soc/qcom/service-notifier.h>
  9. #include "audio_pdr.h"
  10. static struct pd_qmi_client_data audio_pdr_services[AUDIO_PDR_DOMAIN_MAX] = {
  11. { /* AUDIO_PDR_DOMAIN_ADSP */
  12. .client_name = "audio_pdr_adsp",
  13. .service_name = "avs/audio"
  14. }
  15. };
  16. struct srcu_notifier_head audio_pdr_cb_list;
  17. static int audio_pdr_locator_callback(struct notifier_block *this,
  18. unsigned long opcode, void *data)
  19. {
  20. unsigned long pdr_state = AUDIO_PDR_FRAMEWORK_DOWN;
  21. if (opcode == LOCATOR_DOWN) {
  22. pr_debug("%s: Service %s is down!", __func__,
  23. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].
  24. service_name);
  25. goto done;
  26. }
  27. memcpy(&audio_pdr_services, data,
  28. sizeof(audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP]));
  29. if (audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].total_domains == 1) {
  30. int domain_list_size = (sizeof(struct servreg_loc_entry_v01)) *
  31. (audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].total_domains);
  32. struct servreg_loc_entry_v01 *domain_list_temp = NULL;
  33. pr_debug("%s: Service %s, returned total domains %d, ",
  34. __func__,
  35. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].service_name,
  36. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].
  37. total_domains);
  38. domain_list_temp = (struct servreg_loc_entry_v01 *)kzalloc(
  39. sizeof(domain_list_size), GFP_KERNEL);
  40. if(!domain_list_size)
  41. {
  42. pr_err("%s: Service %s total domains %d could not allocate memory",
  43. __func__,
  44. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].service_name,
  45. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].
  46. total_domains);
  47. goto done;
  48. } else {
  49. memcpy(domain_list_temp,
  50. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].domain_list,
  51. domain_list_size);
  52. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].domain_list = domain_list_temp;
  53. }
  54. pdr_state = AUDIO_PDR_FRAMEWORK_UP;
  55. goto done;
  56. } else
  57. pr_err("%s: Service %s returned invalid total domains %d",
  58. __func__,
  59. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].service_name,
  60. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].
  61. total_domains);
  62. done:
  63. srcu_notifier_call_chain(&audio_pdr_cb_list, pdr_state, NULL);
  64. return NOTIFY_OK;
  65. }
  66. static struct notifier_block audio_pdr_locator_nb = {
  67. .notifier_call = audio_pdr_locator_callback,
  68. .priority = 0,
  69. };
  70. /**
  71. * audio_pdr_register -
  72. * register to PDR framework
  73. *
  74. * @nb: notifier block
  75. *
  76. * Returns 0 on success or error on failure
  77. */
  78. int audio_pdr_register(struct notifier_block *nb)
  79. {
  80. if (nb == NULL) {
  81. pr_err("%s: Notifier block is NULL\n", __func__);
  82. return -EINVAL;
  83. }
  84. return srcu_notifier_chain_register(&audio_pdr_cb_list, nb);
  85. }
  86. EXPORT_SYMBOL(audio_pdr_register);
  87. /**
  88. * audio_pdr_deregister -
  89. * Deregister from PDR framework
  90. *
  91. * @nb: notifier block
  92. *
  93. * Returns 0 on success or error on failure
  94. */
  95. int audio_pdr_deregister(struct notifier_block *nb)
  96. {
  97. if (nb == NULL) {
  98. pr_err("%s: Notifier block is NULL\n", __func__);
  99. return -EINVAL;
  100. }
  101. return srcu_notifier_chain_unregister(&audio_pdr_cb_list, nb);
  102. }
  103. EXPORT_SYMBOL(audio_pdr_deregister);
  104. void *audio_pdr_service_register(int domain_id,
  105. struct notifier_block *nb, int *curr_state)
  106. {
  107. void *handle;
  108. if ((domain_id < 0) ||
  109. (domain_id >= AUDIO_PDR_DOMAIN_MAX)) {
  110. pr_err("%s: Invalid service ID %d\n", __func__, domain_id);
  111. return ERR_PTR(-EINVAL);
  112. }
  113. handle = service_notif_register_notifier(
  114. audio_pdr_services[domain_id].domain_list[0].name,
  115. audio_pdr_services[domain_id].domain_list[0].instance_id,
  116. nb, curr_state);
  117. if (IS_ERR_OR_NULL(handle)) {
  118. pr_err("%s: Failed to register for domain %s, instance %d\n",
  119. __func__,
  120. audio_pdr_services[domain_id].domain_list[0].name,
  121. audio_pdr_services[domain_id].domain_list[0].
  122. instance_id);
  123. }
  124. return handle;
  125. }
  126. EXPORT_SYMBOL(audio_pdr_service_register);
  127. int audio_pdr_service_deregister(void *service_handle,
  128. struct notifier_block *nb)
  129. {
  130. int ret;
  131. if (service_handle == NULL) {
  132. pr_err("%s: service handle is NULL\n", __func__);
  133. ret = -EINVAL;
  134. goto done;
  135. }
  136. ret = service_notif_unregister_notifier(
  137. service_handle, nb);
  138. if (ret < 0)
  139. pr_err("%s: Failed to deregister service ret %d\n",
  140. __func__, ret);
  141. done:
  142. return ret;
  143. }
  144. EXPORT_SYMBOL(audio_pdr_service_deregister);
  145. static int __init audio_pdr_subsys_init(void)
  146. {
  147. srcu_init_notifier_head(&audio_pdr_cb_list);
  148. return 0;
  149. }
  150. static int __init audio_pdr_late_init(void)
  151. {
  152. int ret;
  153. audio_pdr_subsys_init();
  154. ret = get_service_location(
  155. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].client_name,
  156. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].service_name,
  157. &audio_pdr_locator_nb);
  158. if (ret < 0) {
  159. pr_err("%s get_service_location failed ret %d\n",
  160. __func__, ret);
  161. srcu_notifier_call_chain(&audio_pdr_cb_list,
  162. AUDIO_PDR_FRAMEWORK_DOWN, NULL);
  163. }
  164. return ret;
  165. }
  166. module_init(audio_pdr_late_init);
  167. static void __exit audio_pdr_late_exit(void)
  168. {
  169. int i;
  170. for (i = 0; i < AUDIO_PDR_DOMAIN_MAX; i++)
  171. {
  172. if (audio_pdr_services[i].domain_list != NULL)
  173. kfree(audio_pdr_services[i].domain_list);
  174. }
  175. }
  176. module_exit(audio_pdr_late_exit);
  177. MODULE_DESCRIPTION("PDR framework driver");
  178. MODULE_LICENSE("GPL v2");