blob: 49564849c690b2a005988a567e17b85be8d9170b [file] [log] [blame]
Kumar Galad12d8af2016-10-05 12:01:54 -05001/*
2 * Copyright (c) 2015-2016 Intel Corporation.
3 *
David B. Kinderac74d8b2017-01-18 17:01:01 -08004 * SPDX-License-Identifier: Apache-2.0
Kumar Galad12d8af2016-10-05 12:01:54 -05005 */
6
Kumar Galad12d8af2016-10-05 12:01:54 -05007#include <string.h>
8#include <device.h>
Anas Nashife1e05a22019-06-25 12:25:32 -04009#include <sys/atomic.h>
Andrew Boie9d148742018-11-12 10:25:12 -080010#include <syscall_handler.h>
Kumar Galad12d8af2016-10-05 12:01:54 -050011
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010012extern const struct init_entry __init_start[];
13extern const struct init_entry __init_PRE_KERNEL_1_start[];
14extern const struct init_entry __init_PRE_KERNEL_2_start[];
15extern const struct init_entry __init_POST_KERNEL_start[];
16extern const struct init_entry __init_APPLICATION_start[];
17extern const struct init_entry __init_end[];
Kumar Galad12d8af2016-10-05 12:01:54 -050018
Daniel Leung4e1637b2020-01-15 08:57:29 -080019#ifdef CONFIG_SMP
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010020extern const struct init_entry __init_SMP_start[];
Daniel Leung4e1637b2020-01-15 08:57:29 -080021#endif
Kumar Galad12d8af2016-10-05 12:01:54 -050022
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020023extern const struct device __device_start[];
24extern const struct device __device_end[];
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010025
Tomasz Bursztykaaac9e2c2020-04-30 11:49:39 +020026extern uint32_t __device_init_status_start[];
27
Kumar Galad12d8af2016-10-05 12:01:54 -050028/**
Peter Bigot1cadd8b2021-02-02 10:07:18 -060029 * @brief Initialize state for all static devices.
30 *
31 * The state object is always zero-initialized, but this may not be
32 * sufficient.
33 */
34void z_device_state_init(void)
35{
36 const struct device *dev = __device_start;
37
38 while (dev < __device_end) {
39 z_object_init(dev);
40 ++dev;
41 }
42}
43
44/**
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010045 * @brief Execute all the init entry initialization functions at a given level
Kumar Galad12d8af2016-10-05 12:01:54 -050046 *
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010047 * @details Invokes the initialization routine for each init entry object
48 * created by the INIT_ENTRY_DEFINE() macro using the specified level.
49 * The linker script places the init entry objects in memory in the order
Kumar Galad12d8af2016-10-05 12:01:54 -050050 * they need to be invoked, with symbols indicating where one level leaves
51 * off and the next one begins.
52 *
53 * @param level init level to run.
54 */
Kumar Galaa1b77fd2020-05-27 11:26:57 -050055void z_sys_init_run_level(int32_t level)
Kumar Galad12d8af2016-10-05 12:01:54 -050056{
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010057 static const struct init_entry *levels[] = {
58 __init_PRE_KERNEL_1_start,
59 __init_PRE_KERNEL_2_start,
60 __init_POST_KERNEL_start,
61 __init_APPLICATION_start,
Daniel Leung4e1637b2020-01-15 08:57:29 -080062#ifdef CONFIG_SMP
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010063 __init_SMP_start,
Daniel Leung4e1637b2020-01-15 08:57:29 -080064#endif
Flavio Ceolinac146852018-11-01 17:42:07 -070065 /* End marker */
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010066 __init_end,
Flavio Ceolinac146852018-11-01 17:42:07 -070067 };
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010068 const struct init_entry *entry;
Kumar Galad12d8af2016-10-05 12:01:54 -050069
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +010070 for (entry = levels[level]; entry < levels[level+1]; entry++) {
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020071 const struct device *dev = entry->dev;
Peter Bigot65eee5c2021-02-02 10:23:55 -060072 int rc = entry->init(dev);
Kumar Galad12d8af2016-10-05 12:01:54 -050073
Peter Bigot65eee5c2021-02-02 10:23:55 -060074 if (dev != NULL) {
75 /* Mark device initialized. If initialization
76 * failed, record the error condition.
Tomasz Bursztykaaac9e2c2020-04-30 11:49:39 +020077 */
Peter Bigot65eee5c2021-02-02 10:23:55 -060078 if (rc != 0) {
79 if (rc < 0) {
80 rc = -rc;
81 }
82 if (rc > UINT8_MAX) {
83 rc = UINT8_MAX;
84 }
85 dev->state->init_res = rc;
86 }
87 dev->state->initialized = true;
Andrew Boiea68120d2018-12-07 13:12:21 -080088 }
Kumar Galad12d8af2016-10-05 12:01:54 -050089 }
90}
91
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020092const struct device *z_impl_device_get_binding(const char *name)
Kumar Galad12d8af2016-10-05 12:01:54 -050093{
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +020094 const struct device *dev;
Kumar Galad12d8af2016-10-05 12:01:54 -050095
Peter Bigot5b36a012021-02-12 06:19:08 -060096 /* A null string identifies no device. So does an empty
97 * string.
98 */
Anas Nashif06304522021-03-29 17:13:18 -040099 if ((name == NULL) || (name[0] == '\0')) {
Peter Bigot5b36a012021-02-12 06:19:08 -0600100 return NULL;
101 }
102
Leandro Pereirab55eb032018-02-14 14:47:11 -0800103 /* Split the search into two loops: in the common scenario, where
104 * device names are stored in ROM (and are referenced by the user
105 * with CONFIG_* macros), only cheap pointer comparisons will be
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +0100106 * performed. Reserve string comparisons for a fallback.
Leandro Pereirab55eb032018-02-14 14:47:11 -0800107 */
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +0100108 for (dev = __device_start; dev != __device_end; dev++) {
Peter Bigotd8b86cb2020-06-22 10:01:39 -0500109 if (z_device_ready(dev) && (dev->name == name)) {
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +0100110 return dev;
Leandro Pereirab55eb032018-02-14 14:47:11 -0800111 }
112 }
113
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +0100114 for (dev = __device_start; dev != __device_end; dev++) {
Peter Bigotd8b86cb2020-06-22 10:01:39 -0500115 if (z_device_ready(dev) && (strcmp(name, dev->name) == 0)) {
Tomasz Bursztyka8d7bb8f2020-03-09 11:02:20 +0100116 return dev;
Kumar Galad12d8af2016-10-05 12:01:54 -0500117 }
118 }
119
120 return NULL;
121}
122
Andrew Boie9d148742018-11-12 10:25:12 -0800123#ifdef CONFIG_USERSPACE
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200124static inline const struct device *z_vrfy_device_get_binding(const char *name)
Andrew Boie9d148742018-11-12 10:25:12 -0800125{
126 char name_copy[Z_DEVICE_MAX_NAME_LEN];
127
128 if (z_user_string_copy(name_copy, (char *)name, sizeof(name_copy))
129 != 0) {
Daniel Leung07734412021-04-27 11:39:33 -0700130 return NULL;
Andrew Boie9d148742018-11-12 10:25:12 -0800131 }
132
Andy Ross65649742019-08-06 13:34:31 -0700133 return z_impl_device_get_binding(name_copy);
Andrew Boie9d148742018-11-12 10:25:12 -0800134}
Andy Ross65649742019-08-06 13:34:31 -0700135#include <syscalls/device_get_binding_mrsh.c>
Andrzej Głąbek6de16d02021-03-12 16:25:59 +0100136
137static inline int z_vrfy_device_usable_check(const struct device *dev)
138{
139 Z_OOPS(Z_SYSCALL_OBJ_INIT(dev, K_OBJ_ANY));
140
141 return z_impl_device_usable_check(dev);
142}
143#include <syscalls/device_usable_check_mrsh.c>
Andrew Boie9d148742018-11-12 10:25:12 -0800144#endif /* CONFIG_USERSPACE */
145
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200146size_t z_device_get_all_static(struct device const **devices)
Peter Bigot219a3ca2020-06-22 08:55:37 -0500147{
148 *devices = __device_start;
149 return __device_end - __device_start;
150}
151
Tomasz Bursztykaaac9e2c2020-04-30 11:49:39 +0200152bool z_device_ready(const struct device *dev)
153{
Armando Visconti4b4068c2021-05-13 18:24:25 +0200154 /*
155 * if an invalid device pointer is passed as argument, this call
156 * reports the `device` as not ready for usage.
157 */
158 if (dev == NULL) {
159 return false;
160 }
161
Anas Nashifbbbc38b2021-03-29 10:03:49 -0400162 return dev->state->initialized && (dev->state->init_res == 0U);
Tomasz Bursztykaaac9e2c2020-04-30 11:49:39 +0200163}
164
Martí Bolívarb94a11f2021-09-24 12:25:40 -0700165int device_required_foreach(const struct device *dev,
166 device_visitor_callback_t visitor_cb,
167 void *context)
Peter Bigotb29abe32021-02-22 11:42:08 -0600168{
Martí Bolívarb94a11f2021-09-24 12:25:40 -0700169 size_t handle_count = 0;
170 const device_handle_t *handles =
171 device_required_handles_get(dev, &handle_count);
172
Peter Bigotb29abe32021-02-22 11:42:08 -0600173 /* Iterate over fixed devices */
174 for (size_t i = 0; i < handle_count; ++i) {
175 device_handle_t dh = handles[i];
176 const struct device *rdev = device_from_handle(dh);
177 int rc = visitor_cb(rdev, context);
178
179 if (rc < 0) {
180 return rc;
181 }
182 }
183
184 return handle_count;
185}