hotplug_manager.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file hotplug manager interface
  8. */
  9. #if defined(CONFIG_SYS_LOG)
  10. #define SYS_LOG_NO_NEWLINE
  11. #ifdef SYS_LOG_DOMAIN
  12. #undef SYS_LOG_DOMAIN
  13. #endif
  14. #define SYS_LOG_DOMAIN "hotpug_manager"
  15. #endif
  16. #include <os_common_api.h>
  17. #include <msg_manager.h>
  18. #include <mem_manager.h>
  19. #include <sys_monitor.h>
  20. #include <string.h>
  21. #include <hotplug_manager.h>
  22. #ifdef CONFIG_TWS
  23. #include "bt_manager.h"
  24. #endif
  25. static struct hotplug_manager_context_t hotplug_manager_context;
  26. static struct hotplug_manager_context_t *hotplug_manager;
  27. int hotplug_device_register(const struct hotplug_device_t *device)
  28. {
  29. const struct hotplug_device_t *temp_device = NULL;
  30. int free_index = -1;
  31. for (int i = 0; i < MAX_HOTPLUG_DEVICE_NUM; i++) {
  32. temp_device = hotplug_manager->device[i];
  33. if (temp_device && temp_device->type == device->type) {
  34. return -EEXIST;
  35. } else if (!temp_device) {
  36. free_index = i;
  37. break;
  38. }
  39. }
  40. if (free_index != -1 && free_index < MAX_HOTPLUG_DEVICE_NUM) {
  41. hotplug_manager->device[free_index] = device;
  42. hotplug_manager->device_num++;
  43. }
  44. return 0;
  45. }
  46. int hotplug_device_unregister(const struct hotplug_device_t *device)
  47. {
  48. const struct hotplug_device_t *temp_device = NULL;
  49. for (int i = 0; i < MAX_HOTPLUG_DEVICE_NUM; i++) {
  50. temp_device = hotplug_manager->device[i];
  51. if (temp_device && temp_device->type == device->type) {
  52. hotplug_manager->device[i] = NULL;
  53. hotplug_manager->device_num--;
  54. }
  55. }
  56. return 0;
  57. }
  58. int hotplug_event_report(int device_type, int device_state)
  59. {
  60. struct app_msg msg = {0};
  61. msg.type = MSG_HOTPLUG_EVENT;
  62. msg.cmd = device_type;
  63. msg.value = device_state;
  64. SYS_LOG_INF("type: %d state: %d\n", device_type, device_state);
  65. return send_async_msg("main", &msg);
  66. }
  67. static int _hotplug_manager_work_handle(void)
  68. {
  69. int state = HOTPLUG_NONE;
  70. const struct hotplug_device_t *device = NULL;
  71. /**slave not report hot plug*/
  72. #ifdef CONFIG_TWS
  73. if (bt_manager_tws_get_dev_role() == BTSRV_TWS_SLAVE) {
  74. return 0;
  75. }
  76. #endif
  77. for (int i = 0; i < MAX_HOTPLUG_DEVICE_NUM; i++) {
  78. device = hotplug_manager->device[i];
  79. if (!device)
  80. continue;
  81. if (device->hotplug_detect) {
  82. state = device->hotplug_detect();
  83. }
  84. if (state != HOTPLUG_NONE) {
  85. if (device->fs_process) {
  86. if (device->fs_process(state)) {
  87. continue;
  88. }
  89. }
  90. if (device->get_type) {
  91. hotplug_event_report(device->get_type(), state);
  92. } else {
  93. hotplug_event_report(device->type, state);
  94. }
  95. }
  96. }
  97. return 0;
  98. }
  99. int hotplug_manager_get_state(int hotplug_device_type)
  100. {
  101. int state = HOTPLUG_NONE;
  102. const struct hotplug_device_t *device = NULL;
  103. for (int i = 0; i < MAX_HOTPLUG_DEVICE_NUM; i++) {
  104. device = hotplug_manager->device[i];
  105. if (device->type == hotplug_device_type) {
  106. state = device->get_state();
  107. break;
  108. }
  109. }
  110. return state;
  111. }
  112. int hotplug_manager_init(void)
  113. {
  114. if (hotplug_manager)
  115. return -EEXIST;
  116. hotplug_manager = &hotplug_manager_context;
  117. memset(hotplug_manager, 0, sizeof(struct hotplug_manager_context_t));
  118. #ifdef CONFIG_LINEIN_HOTPLUG
  119. hotplug_linein_init();
  120. #endif
  121. #ifdef CONFIG_CARD_HOTPLUG
  122. hotplug_sdcard_init();
  123. #endif
  124. #if defined(CONFIG_USB_HOTPLUG)
  125. hotplug_usb_init();
  126. #endif
  127. #ifdef CONFIG_CHARGER_HOTPLUG
  128. hotplug_charger_init();
  129. #endif
  130. sys_monitor_add_work(_hotplug_manager_work_handle);
  131. return 0;
  132. }