audio_pdr.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <soc/qcom/service-locator.h>
  15. #include <soc/qcom/service-notifier.h>
  16. #include "audio_pdr.h"
  17. static struct pd_qmi_client_data audio_pdr_services[AUDIO_PDR_DOMAIN_MAX] = {
  18. { /* AUDIO_PDR_DOMAIN_ADSP */
  19. .client_name = "audio_pdr_adsp",
  20. .service_name = "avs/audio"
  21. }
  22. };
  23. struct srcu_notifier_head audio_pdr_cb_list;
  24. static int audio_pdr_locator_callback(struct notifier_block *this,
  25. unsigned long opcode, void *data)
  26. {
  27. unsigned long pdr_state = AUDIO_PDR_FRAMEWORK_DOWN;
  28. if (opcode == LOCATOR_DOWN) {
  29. pr_debug("%s: Service %s is down!", __func__,
  30. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].
  31. service_name);
  32. goto done;
  33. }
  34. memcpy(&audio_pdr_services, data,
  35. sizeof(audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP]));
  36. if (audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].total_domains == 1) {
  37. pr_debug("%s: Service %s, returned total domains %d, ",
  38. __func__,
  39. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].service_name,
  40. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].
  41. total_domains);
  42. pdr_state = AUDIO_PDR_FRAMEWORK_UP;
  43. goto done;
  44. } else
  45. pr_err("%s: Service %s returned invalid total domains %d",
  46. __func__,
  47. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].service_name,
  48. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].
  49. total_domains);
  50. done:
  51. srcu_notifier_call_chain(&audio_pdr_cb_list, pdr_state, NULL);
  52. return NOTIFY_OK;
  53. }
  54. static struct notifier_block audio_pdr_locator_nb = {
  55. .notifier_call = audio_pdr_locator_callback,
  56. .priority = 0,
  57. };
  58. /**
  59. * audio_pdr_register -
  60. * register to PDR framework
  61. *
  62. * @nb: notifier block
  63. *
  64. * Returns 0 on success or error on failure
  65. */
  66. int audio_pdr_register(struct notifier_block *nb)
  67. {
  68. if (nb == NULL) {
  69. pr_err("%s: Notifier block is NULL\n", __func__);
  70. return -EINVAL;
  71. }
  72. return srcu_notifier_chain_register(&audio_pdr_cb_list, nb);
  73. }
  74. EXPORT_SYMBOL(audio_pdr_register);
  75. /**
  76. * audio_pdr_deregister -
  77. * Deregister from PDR framework
  78. *
  79. * @nb: notifier block
  80. *
  81. * Returns 0 on success or error on failure
  82. */
  83. int audio_pdr_deregister(struct notifier_block *nb)
  84. {
  85. if (nb == NULL) {
  86. pr_err("%s: Notifier block is NULL\n", __func__);
  87. return -EINVAL;
  88. }
  89. return srcu_notifier_chain_unregister(&audio_pdr_cb_list, nb);
  90. }
  91. EXPORT_SYMBOL(audio_pdr_deregister);
  92. void *audio_pdr_service_register(int domain_id,
  93. struct notifier_block *nb, int *curr_state)
  94. {
  95. void *handle;
  96. if ((domain_id < 0) ||
  97. (domain_id >= AUDIO_PDR_DOMAIN_MAX)) {
  98. pr_err("%s: Invalid service ID %d\n", __func__, domain_id);
  99. return ERR_PTR(-EINVAL);
  100. }
  101. handle = service_notif_register_notifier(
  102. audio_pdr_services[domain_id].domain_list[0].name,
  103. audio_pdr_services[domain_id].domain_list[0].instance_id,
  104. nb, curr_state);
  105. if (IS_ERR_OR_NULL(handle)) {
  106. pr_err("%s: Failed to register for service %s, instance %d\n",
  107. __func__,
  108. audio_pdr_services[domain_id].domain_list[0].name,
  109. audio_pdr_services[domain_id].domain_list[0].
  110. instance_id);
  111. }
  112. return handle;
  113. }
  114. EXPORT_SYMBOL(audio_pdr_service_register);
  115. int audio_pdr_service_deregister(void *service_handle,
  116. struct notifier_block *nb)
  117. {
  118. int ret;
  119. if (service_handle == NULL) {
  120. pr_err("%s: service handle is NULL\n", __func__);
  121. ret = -EINVAL;
  122. goto done;
  123. }
  124. ret = service_notif_unregister_notifier(
  125. service_handle, nb);
  126. if (ret < 0)
  127. pr_err("%s: Failed to deregister service ret %d\n",
  128. __func__, ret);
  129. done:
  130. return ret;
  131. }
  132. EXPORT_SYMBOL(audio_pdr_service_deregister);
  133. static int __init audio_pdr_subsys_init(void)
  134. {
  135. srcu_init_notifier_head(&audio_pdr_cb_list);
  136. return 0;
  137. }
  138. static int __init audio_pdr_late_init(void)
  139. {
  140. int ret;
  141. audio_pdr_subsys_init();
  142. ret = get_service_location(
  143. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].client_name,
  144. audio_pdr_services[AUDIO_PDR_DOMAIN_ADSP].service_name,
  145. &audio_pdr_locator_nb);
  146. if (ret < 0) {
  147. pr_err("%s get_service_location failed ret %d\n",
  148. __func__, ret);
  149. srcu_notifier_call_chain(&audio_pdr_cb_list,
  150. AUDIO_PDR_FRAMEWORK_DOWN, NULL);
  151. }
  152. return ret;
  153. }
  154. module_init(audio_pdr_late_init);
  155. static void __exit audio_pdr_late_exit(void)
  156. {
  157. }
  158. module_exit(audio_pdr_late_exit);
  159. MODULE_DESCRIPTION("PDR framework driver");
  160. MODULE_LICENSE("GPL v2");