hotplug_card.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright (c) 2019 Actions Semiconductor Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * @file hotplug sdcard 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 <string.h>
  19. #include <fs_manager.h>
  20. #include <hotplug_manager.h>
  21. struct sdcard_detect_state_t {
  22. uint8_t prev_state;
  23. uint8_t stable_state;
  24. uint8_t detect_cnt;
  25. };
  26. static struct sdcard_detect_state_t sdcard_detect_state;
  27. int _sdcard_get_state(void)
  28. {
  29. uint8_t stat = STA_NODISK; /* STA_DISK_OK; */
  30. int sdcard_state = HOTPLUG_NONE;
  31. fs_disk_detect("/SD:/", &stat);
  32. if (stat == STA_DISK_OK) {
  33. sdcard_state = HOTPLUG_IN;
  34. } else {
  35. sdcard_state = HOTPLUG_OUT;
  36. }
  37. return sdcard_state;
  38. }
  39. int _sdcard_hotplug_detect(void)
  40. {
  41. int report_state = HOTPLUG_NONE;
  42. int state = HOTPLUG_NONE;
  43. state = _sdcard_get_state();
  44. if (state <= HOTPLUG_NONE) {
  45. goto exit;
  46. }
  47. if (state == sdcard_detect_state.prev_state) {
  48. sdcard_detect_state.detect_cnt++;
  49. if (sdcard_detect_state.detect_cnt >= 1) {
  50. sdcard_detect_state.detect_cnt = 0;
  51. if (state != sdcard_detect_state.stable_state) {
  52. sdcard_detect_state.stable_state = state;
  53. report_state = sdcard_detect_state.stable_state;
  54. }
  55. }
  56. } else {
  57. sdcard_detect_state.detect_cnt = 0;
  58. sdcard_detect_state.prev_state = state;
  59. }
  60. exit:
  61. return report_state;
  62. }
  63. static int _sdcard_hotplug_fs_process(int device_state)
  64. {
  65. int res = -1;
  66. switch (device_state) {
  67. case HOTPLUG_IN:
  68. #ifdef CONFIG_FS_MANAGER
  69. //res = fs_manager_disk_init("SD:");
  70. /**already muount fs when fs_disk_detect */
  71. res = 0;
  72. #endif
  73. break;
  74. case HOTPLUG_OUT:
  75. #ifdef CONFIG_FS_MANAGER
  76. res = fs_manager_disk_uninit("SD:");
  77. #endif
  78. break;
  79. default:
  80. break;
  81. }
  82. return res;
  83. }
  84. static const struct hotplug_device_t sdcard_hotplug_device = {
  85. .type = HOTPLUG_SDCARD,
  86. .get_state = _sdcard_get_state,
  87. .hotplug_detect = _sdcard_hotplug_detect,
  88. .fs_process = _sdcard_hotplug_fs_process,
  89. };
  90. int hotplug_sdcard_init(void)
  91. {
  92. memset(&sdcard_detect_state, 0, sizeof(struct sdcard_detect_state_t));
  93. #ifdef CONFIG_MUTIPLE_VOLUME_MANAGER
  94. if (fs_manager_get_volume_state("SD:")) {
  95. sdcard_detect_state.stable_state = HOTPLUG_IN;
  96. } else {
  97. sdcard_detect_state.stable_state = HOTPLUG_OUT;
  98. }
  99. #endif
  100. return hotplug_device_register(&sdcard_hotplug_device);
  101. }