2016-10-06 01:01:54 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015-2016 Intel Corporation.
|
|
|
|
*
|
2017-01-19 09:01:01 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
2016-10-06 01:01:54 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <device.h>
|
2019-06-26 22:33:55 +08:00
|
|
|
#include <sys/util.h>
|
2019-06-26 00:25:32 +08:00
|
|
|
#include <sys/atomic.h>
|
2018-11-13 02:25:12 +08:00
|
|
|
#include <syscall_handler.h>
|
2016-10-06 01:01:54 +08:00
|
|
|
|
|
|
|
extern struct device __device_init_start[];
|
2016-11-09 03:06:55 +08:00
|
|
|
extern struct device __device_PRE_KERNEL_1_start[];
|
|
|
|
extern struct device __device_PRE_KERNEL_2_start[];
|
|
|
|
extern struct device __device_POST_KERNEL_start[];
|
|
|
|
extern struct device __device_APPLICATION_start[];
|
2016-10-06 01:01:54 +08:00
|
|
|
extern struct device __device_init_end[];
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
|
2017-04-21 23:55:34 +08:00
|
|
|
extern u32_t __device_busy_start[];
|
|
|
|
extern u32_t __device_busy_end[];
|
2016-10-06 01:01:54 +08:00
|
|
|
#define DEVICE_BUSY_SIZE (__device_busy_end - __device_busy_start)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Execute all the device initialization functions at a given level
|
|
|
|
*
|
|
|
|
* @details Invokes the initialization routine for each device object
|
|
|
|
* created by the DEVICE_INIT() macro using the specified level.
|
|
|
|
* The linker script places the device objects in memory in the order
|
|
|
|
* they need to be invoked, with symbols indicating where one level leaves
|
|
|
|
* off and the next one begins.
|
|
|
|
*
|
|
|
|
* @param level init level to run.
|
|
|
|
*/
|
2019-03-09 05:19:05 +08:00
|
|
|
void z_sys_device_do_config_level(s32_t level)
|
2016-10-06 01:01:54 +08:00
|
|
|
{
|
|
|
|
struct device *info;
|
2018-11-02 08:42:07 +08:00
|
|
|
static struct device *config_levels[] = {
|
|
|
|
__device_PRE_KERNEL_1_start,
|
|
|
|
__device_PRE_KERNEL_2_start,
|
|
|
|
__device_POST_KERNEL_start,
|
|
|
|
__device_APPLICATION_start,
|
|
|
|
/* End marker */
|
|
|
|
__device_init_end,
|
|
|
|
};
|
2016-10-06 01:01:54 +08:00
|
|
|
|
2017-03-22 20:49:34 +08:00
|
|
|
for (info = config_levels[level]; info < config_levels[level+1];
|
|
|
|
info++) {
|
2018-12-08 05:12:21 +08:00
|
|
|
int retval;
|
2018-10-25 14:34:25 +08:00
|
|
|
struct device_config *device_conf = info->config;
|
2016-10-06 01:01:54 +08:00
|
|
|
|
2018-12-08 05:12:21 +08:00
|
|
|
retval = device_conf->init(info);
|
|
|
|
if (retval != 0) {
|
|
|
|
/* Initialization failed. Clear the API struct so that
|
|
|
|
* device_get_binding() will not succeed for it.
|
|
|
|
*/
|
|
|
|
info->driver_api = NULL;
|
|
|
|
} else {
|
2019-03-09 05:19:05 +08:00
|
|
|
z_object_init(info);
|
2018-12-08 05:12:21 +08:00
|
|
|
}
|
2016-10-06 01:01:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-09 05:19:05 +08:00
|
|
|
struct device *z_impl_device_get_binding(const char *name)
|
2016-10-06 01:01:54 +08:00
|
|
|
{
|
|
|
|
struct device *info;
|
|
|
|
|
2018-02-15 06:47:11 +08:00
|
|
|
/* Split the search into two loops: in the common scenario, where
|
|
|
|
* device names are stored in ROM (and are referenced by the user
|
|
|
|
* with CONFIG_* macros), only cheap pointer comparisons will be
|
|
|
|
* performed. Reserve string comparisons for a fallback.
|
|
|
|
*/
|
|
|
|
for (info = __device_init_start; info != __device_init_end; info++) {
|
2018-10-25 14:34:25 +08:00
|
|
|
if ((info->driver_api != NULL) &&
|
|
|
|
(info->config->name == name)) {
|
2018-02-15 06:47:11 +08:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-06 01:01:54 +08:00
|
|
|
for (info = __device_init_start; info != __device_init_end; info++) {
|
2018-10-25 14:34:25 +08:00
|
|
|
if (info->driver_api == NULL) {
|
2017-10-20 05:17:37 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-10-25 14:34:25 +08:00
|
|
|
if (strcmp(name, info->config->name) == 0) {
|
2016-10-06 01:01:54 +08:00
|
|
|
return info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-11-13 02:25:12 +08:00
|
|
|
#ifdef CONFIG_USERSPACE
|
|
|
|
Z_SYSCALL_HANDLER(device_get_binding, name)
|
|
|
|
{
|
|
|
|
char name_copy[Z_DEVICE_MAX_NAME_LEN];
|
|
|
|
|
|
|
|
if (z_user_string_copy(name_copy, (char *)name, sizeof(name_copy))
|
|
|
|
!= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-09 05:19:05 +08:00
|
|
|
return (u32_t)z_impl_device_get_binding(name_copy);
|
2018-11-13 02:25:12 +08:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_USERSPACE */
|
|
|
|
|
2016-10-06 01:01:54 +08:00
|
|
|
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
|
2016-10-08 08:07:04 +08:00
|
|
|
int device_pm_control_nop(struct device *unused_device,
|
2019-02-14 12:05:42 +08:00
|
|
|
u32_t unused_ctrl_command,
|
|
|
|
void *unused_context,
|
|
|
|
device_pm_cb cb,
|
|
|
|
void *unused_arg)
|
2016-10-06 01:01:54 +08:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2016-10-08 08:07:04 +08:00
|
|
|
|
2016-10-06 01:01:54 +08:00
|
|
|
void device_list_get(struct device **device_list, int *device_count)
|
|
|
|
{
|
|
|
|
|
|
|
|
*device_list = __device_init_start;
|
|
|
|
*device_count = __device_init_end - __device_init_start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int device_any_busy_check(void)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < DEVICE_BUSY_SIZE; i++) {
|
2019-03-27 09:57:45 +08:00
|
|
|
if (__device_busy_start[i] != 0U) {
|
2016-10-06 01:01:54 +08:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int device_busy_check(struct device *chk_dev)
|
|
|
|
{
|
|
|
|
if (atomic_test_bit((const atomic_t *)__device_busy_start,
|
|
|
|
(chk_dev - __device_init_start))) {
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void device_busy_set(struct device *busy_dev)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
|
|
|
|
atomic_set_bit((atomic_t *) __device_busy_start,
|
|
|
|
(busy_dev - __device_init_start));
|
2016-12-11 14:19:26 +08:00
|
|
|
#else
|
|
|
|
ARG_UNUSED(busy_dev);
|
2016-10-06 01:01:54 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void device_busy_clear(struct device *busy_dev)
|
|
|
|
{
|
|
|
|
#ifdef CONFIG_DEVICE_POWER_MANAGEMENT
|
|
|
|
atomic_clear_bit((atomic_t *) __device_busy_start,
|
|
|
|
(busy_dev - __device_init_start));
|
2016-12-11 14:19:26 +08:00
|
|
|
#else
|
|
|
|
ARG_UNUSED(busy_dev);
|
2016-10-06 01:01:54 +08:00
|
|
|
#endif
|
|
|
|
}
|