device.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * Copyright (c) 2015-2016 Intel Corporation.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <device.h>
  8. #include <sys/atomic.h>
  9. #include <syscall_handler.h>
  10. extern const struct init_entry __init_start[];
  11. extern const struct init_entry __init_PRE_KERNEL_1_start[];
  12. extern const struct init_entry __init_PRE_KERNEL_2_start[];
  13. extern const struct init_entry __init_POST_KERNEL_start[];
  14. extern const struct init_entry __init_APPLICATION_start[];
  15. extern const struct init_entry __init_POWER_MAX_start[];
  16. extern const struct init_entry __init_end[];
  17. #ifdef CONFIG_SMP
  18. extern const struct init_entry __init_SMP_start[];
  19. #endif
  20. extern const struct device __device_start[];
  21. extern const struct device __device_end[];
  22. extern uint32_t __device_init_status_start[];
  23. /**
  24. * @brief Initialize state for all static devices.
  25. *
  26. * The state object is always zero-initialized, but this may not be
  27. * sufficient.
  28. */
  29. void z_device_state_init(void)
  30. {
  31. const struct device *dev = __device_start;
  32. while (dev < __device_end) {
  33. z_object_init(dev);
  34. ++dev;
  35. }
  36. }
  37. /**
  38. * @brief Execute all the init entry initialization functions at a given level
  39. *
  40. * @details Invokes the initialization routine for each init entry object
  41. * created by the INIT_ENTRY_DEFINE() macro using the specified level.
  42. * The linker script places the init entry objects in memory in the order
  43. * they need to be invoked, with symbols indicating where one level leaves
  44. * off and the next one begins.
  45. *
  46. * @param level init level to run.
  47. */
  48. void z_sys_init_run_level(int32_t level)
  49. {
  50. static const struct init_entry *levels[] = {
  51. __init_PRE_KERNEL_1_start,
  52. __init_PRE_KERNEL_2_start,
  53. __init_POST_KERNEL_start,
  54. __init_APPLICATION_start,
  55. #ifdef CONFIG_SMP
  56. __init_SMP_start,
  57. #endif
  58. __init_POWER_MAX_start,
  59. /* End marker */
  60. __init_end,
  61. };
  62. const struct init_entry *entry;
  63. for (entry = levels[level]; entry < levels[level+1]; entry++) {
  64. const struct device *dev = entry->dev;
  65. #ifdef CONFIG_DEVICE_DEBUG
  66. if(dev){
  67. printk("z_sys_init dev(%s) func:%p\n", dev->name, entry->init);
  68. }else{
  69. printk("z_sys_init entry(%p) func:%p\n", entry, entry->init);
  70. }
  71. #endif
  72. int rc = entry->init(dev);
  73. if (dev != NULL) {
  74. /* Mark device initialized. If initialization
  75. * failed, record the error condition.
  76. */
  77. if (rc != 0) {
  78. if (rc < 0) {
  79. rc = -rc;
  80. }
  81. if (rc > UINT8_MAX) {
  82. rc = UINT8_MAX;
  83. }
  84. dev->state->init_res = rc;
  85. }
  86. dev->state->initialized = true;
  87. }
  88. }
  89. }
  90. const struct device *z_impl_device_get_binding(const char *name)
  91. {
  92. const struct device *dev;
  93. /* A null string identifies no device. So does an empty
  94. * string.
  95. */
  96. if ((name == NULL) || (name[0] == '\0')) {
  97. return NULL;
  98. }
  99. /* Split the search into two loops: in the common scenario, where
  100. * device names are stored in ROM (and are referenced by the user
  101. * with CONFIG_* macros), only cheap pointer comparisons will be
  102. * performed. Reserve string comparisons for a fallback.
  103. */
  104. for (dev = __device_start; dev != __device_end; dev++) {
  105. if (z_device_ready(dev) && (dev->name == name)) {
  106. return dev;
  107. }
  108. }
  109. for (dev = __device_start; dev != __device_end; dev++) {
  110. if (z_device_ready(dev) && (strcmp(name, dev->name) == 0)) {
  111. return dev;
  112. }
  113. }
  114. #ifdef CONFIG_DEVICE_DEBUG
  115. printk("!!!ERR: dev %s not found\n", name);
  116. #endif
  117. return NULL;
  118. }
  119. #ifdef CONFIG_USERSPACE
  120. static inline const struct device *z_vrfy_device_get_binding(const char *name)
  121. {
  122. char name_copy[Z_DEVICE_MAX_NAME_LEN];
  123. if (z_user_string_copy(name_copy, (char *)name, sizeof(name_copy))
  124. != 0) {
  125. return NULL;
  126. }
  127. return z_impl_device_get_binding(name_copy);
  128. }
  129. #include <syscalls/device_get_binding_mrsh.c>
  130. static inline int z_vrfy_device_usable_check(const struct device *dev)
  131. {
  132. Z_OOPS(Z_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
  133. return z_impl_device_usable_check(dev);
  134. }
  135. #include <syscalls/device_usable_check_mrsh.c>
  136. #endif /* CONFIG_USERSPACE */
  137. size_t z_device_get_all_static(struct device const **devices)
  138. {
  139. *devices = __device_start;
  140. return __device_end - __device_start;
  141. }
  142. bool z_device_ready(const struct device *dev)
  143. {
  144. /*
  145. * if an invalid device pointer is passed as argument, this call
  146. * reports the `device` as not ready for usage.
  147. */
  148. if (dev == NULL) {
  149. return false;
  150. }
  151. return dev->state->initialized && (dev->state->init_res == 0U);
  152. }
  153. int device_required_foreach(const struct device *dev,
  154. device_visitor_callback_t visitor_cb,
  155. void *context)
  156. {
  157. size_t handle_count = 0;
  158. const device_handle_t *handles =
  159. device_required_handles_get(dev, &handle_count);
  160. /* Iterate over fixed devices */
  161. for (size_t i = 0; i < handle_count; ++i) {
  162. device_handle_t dh = handles[i];
  163. const struct device *rdev = device_from_handle(dh);
  164. int rc = visitor_cb(rdev, context);
  165. if (rc < 0) {
  166. return rc;
  167. }
  168. }
  169. return handle_count;
  170. }