Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2016 Intel Corporation. |
| 3 | * |
David B. Kinder | ac74d8b | 2017-01-18 17:01:01 -0800 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 5 | */ |
| 6 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 7 | #include <string.h> |
| 8 | #include <device.h> |
Anas Nashif | e1e05a2 | 2019-06-25 12:25:32 -0400 | [diff] [blame] | 9 | #include <sys/atomic.h> |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 10 | #include <syscall_handler.h> |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 11 | |
| 12 | extern struct device __device_init_start[]; |
Andrew Boie | 0b474ee | 2016-11-08 11:06:55 -0800 | [diff] [blame] | 13 | extern struct device __device_PRE_KERNEL_1_start[]; |
| 14 | extern struct device __device_PRE_KERNEL_2_start[]; |
| 15 | extern struct device __device_POST_KERNEL_start[]; |
| 16 | extern struct device __device_APPLICATION_start[]; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 17 | extern struct device __device_init_end[]; |
| 18 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 19 | |
| 20 | #ifdef CONFIG_DEVICE_POWER_MANAGEMENT |
Kumar Gala | cc334c7 | 2017-04-21 10:55:34 -0500 | [diff] [blame] | 21 | extern u32_t __device_busy_start[]; |
| 22 | extern u32_t __device_busy_end[]; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 23 | #define DEVICE_BUSY_SIZE (__device_busy_end - __device_busy_start) |
| 24 | #endif |
| 25 | |
| 26 | /** |
| 27 | * @brief Execute all the device initialization functions at a given level |
| 28 | * |
| 29 | * @details Invokes the initialization routine for each device object |
| 30 | * created by the DEVICE_INIT() macro using the specified level. |
| 31 | * The linker script places the device objects in memory in the order |
| 32 | * they need to be invoked, with symbols indicating where one level leaves |
| 33 | * off and the next one begins. |
| 34 | * |
| 35 | * @param level init level to run. |
| 36 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 37 | void z_sys_device_do_config_level(s32_t level) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 38 | { |
| 39 | struct device *info; |
Flavio Ceolin | ac14685 | 2018-11-01 17:42:07 -0700 | [diff] [blame] | 40 | static struct device *config_levels[] = { |
| 41 | __device_PRE_KERNEL_1_start, |
| 42 | __device_PRE_KERNEL_2_start, |
| 43 | __device_POST_KERNEL_start, |
| 44 | __device_APPLICATION_start, |
| 45 | /* End marker */ |
| 46 | __device_init_end, |
| 47 | }; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 48 | |
Amir Kaplan | 61b6f5a | 2017-03-22 14:49:34 +0200 | [diff] [blame] | 49 | for (info = config_levels[level]; info < config_levels[level+1]; |
| 50 | info++) { |
Andrew Boie | a68120d | 2018-12-07 13:12:21 -0800 | [diff] [blame] | 51 | int retval; |
Krzysztof Chruscinski | a8b5a2e | 2020-01-21 08:26:19 +0100 | [diff] [blame] | 52 | const struct device_config *device_conf = info->config; |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 53 | |
Andrew Boie | a68120d | 2018-12-07 13:12:21 -0800 | [diff] [blame] | 54 | retval = device_conf->init(info); |
| 55 | if (retval != 0) { |
| 56 | /* Initialization failed. Clear the API struct so that |
| 57 | * device_get_binding() will not succeed for it. |
| 58 | */ |
| 59 | info->driver_api = NULL; |
| 60 | } else { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 61 | z_object_init(info); |
Andrew Boie | a68120d | 2018-12-07 13:12:21 -0800 | [diff] [blame] | 62 | } |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 66 | struct device *z_impl_device_get_binding(const char *name) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 67 | { |
| 68 | struct device *info; |
| 69 | |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 70 | /* Split the search into two loops: in the common scenario, where |
| 71 | * device names are stored in ROM (and are referenced by the user |
| 72 | * with CONFIG_* macros), only cheap pointer comparisons will be |
| 73 | * performed. Reserve string comparisons for a fallback. |
| 74 | */ |
| 75 | for (info = __device_init_start; info != __device_init_end; info++) { |
Adithya Baglody | 8feda92 | 2018-10-25 12:04:25 +0530 | [diff] [blame] | 76 | if ((info->driver_api != NULL) && |
| 77 | (info->config->name == name)) { |
Leandro Pereira | b55eb03 | 2018-02-14 14:47:11 -0800 | [diff] [blame] | 78 | return info; |
| 79 | } |
| 80 | } |
| 81 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 82 | for (info = __device_init_start; info != __device_init_end; info++) { |
Adithya Baglody | 8feda92 | 2018-10-25 12:04:25 +0530 | [diff] [blame] | 83 | if (info->driver_api == NULL) { |
Leandro Pereira | d24daa4 | 2017-10-19 14:17:37 -0700 | [diff] [blame] | 84 | continue; |
| 85 | } |
| 86 | |
Adithya Baglody | 8feda92 | 2018-10-25 12:04:25 +0530 | [diff] [blame] | 87 | if (strcmp(name, info->config->name) == 0) { |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 88 | return info; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | return NULL; |
| 93 | } |
| 94 | |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 95 | #ifdef CONFIG_USERSPACE |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 96 | static inline struct device *z_vrfy_device_get_binding(const char *name) |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 97 | { |
| 98 | char name_copy[Z_DEVICE_MAX_NAME_LEN]; |
| 99 | |
| 100 | if (z_user_string_copy(name_copy, (char *)name, sizeof(name_copy)) |
| 101 | != 0) { |
| 102 | return 0; |
| 103 | } |
| 104 | |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 105 | return z_impl_device_get_binding(name_copy); |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 106 | } |
Andy Ross | 6564974 | 2019-08-06 13:34:31 -0700 | [diff] [blame] | 107 | #include <syscalls/device_get_binding_mrsh.c> |
Andrew Boie | 9d14874 | 2018-11-12 10:25:12 -0800 | [diff] [blame] | 108 | #endif /* CONFIG_USERSPACE */ |
| 109 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 110 | #ifdef CONFIG_DEVICE_POWER_MANAGEMENT |
Ramesh Thomas | 6249c56 | 2016-10-07 17:07:04 -0700 | [diff] [blame] | 111 | int device_pm_control_nop(struct device *unused_device, |
Ramakrishna Pallala | e1639b5 | 2019-02-14 09:35:42 +0530 | [diff] [blame] | 112 | u32_t unused_ctrl_command, |
| 113 | void *unused_context, |
| 114 | device_pm_cb cb, |
| 115 | void *unused_arg) |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 116 | { |
| 117 | return 0; |
| 118 | } |
Ramesh Thomas | 6249c56 | 2016-10-07 17:07:04 -0700 | [diff] [blame] | 119 | |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 120 | void device_list_get(struct device **device_list, int *device_count) |
| 121 | { |
| 122 | |
| 123 | *device_list = __device_init_start; |
| 124 | *device_count = __device_init_end - __device_init_start; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | int device_any_busy_check(void) |
| 129 | { |
| 130 | int i = 0; |
| 131 | |
| 132 | for (i = 0; i < DEVICE_BUSY_SIZE; i++) { |
Patrik Flykt | 24d7143 | 2019-03-26 19:57:45 -0600 | [diff] [blame] | 133 | if (__device_busy_start[i] != 0U) { |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 134 | return -EBUSY; |
| 135 | } |
| 136 | } |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | int device_busy_check(struct device *chk_dev) |
| 141 | { |
| 142 | if (atomic_test_bit((const atomic_t *)__device_busy_start, |
| 143 | (chk_dev - __device_init_start))) { |
| 144 | return -EBUSY; |
| 145 | } |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | #endif |
| 150 | |
| 151 | void device_busy_set(struct device *busy_dev) |
| 152 | { |
| 153 | #ifdef CONFIG_DEVICE_POWER_MANAGEMENT |
| 154 | atomic_set_bit((atomic_t *) __device_busy_start, |
| 155 | (busy_dev - __device_init_start)); |
Flavio Santes | b80db0a | 2016-12-11 00:19:26 -0600 | [diff] [blame] | 156 | #else |
| 157 | ARG_UNUSED(busy_dev); |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 158 | #endif |
| 159 | } |
| 160 | |
| 161 | void device_busy_clear(struct device *busy_dev) |
| 162 | { |
| 163 | #ifdef CONFIG_DEVICE_POWER_MANAGEMENT |
| 164 | atomic_clear_bit((atomic_t *) __device_busy_start, |
| 165 | (busy_dev - __device_init_start)); |
Flavio Santes | b80db0a | 2016-12-11 00:19:26 -0600 | [diff] [blame] | 166 | #else |
| 167 | ARG_UNUSED(busy_dev); |
Kumar Gala | d12d8af | 2016-10-05 12:01:54 -0500 | [diff] [blame] | 168 | #endif |
| 169 | } |