2018-10-05 09:38:55 -05:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
|
|
|
* Copyright (c) 2016 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/shell/shell.h>
|
|
|
|
#include <zephyr/init.h>
|
2018-10-05 09:38:55 -05:00
|
|
|
#include <string.h>
|
2020-12-08 09:03:57 -06:00
|
|
|
#include <stdio.h>
|
2022-05-06 11:12:04 +02:00
|
|
|
#include <zephyr/device.h>
|
|
|
|
#include <zephyr/pm/device.h>
|
2023-03-21 12:10:19 -05:00
|
|
|
#include <zephyr/pm/device_runtime.h>
|
2023-11-08 08:54:57 -08:00
|
|
|
#include <zephyr/arch/arch_interface.h>
|
2018-10-05 09:38:55 -05:00
|
|
|
|
2021-02-11 14:39:25 -06:00
|
|
|
static const char *get_device_name(const struct device *dev,
|
|
|
|
char *buf,
|
|
|
|
size_t len)
|
|
|
|
{
|
|
|
|
const char *name = dev->name;
|
|
|
|
|
|
|
|
if ((name == NULL) || (name[0] == 0)) {
|
|
|
|
snprintf(buf, len, "[%p]", dev);
|
|
|
|
name = buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
return name;
|
|
|
|
}
|
2023-06-14 14:30:41 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEVICE_DEPS
|
2021-02-22 11:42:50 -06:00
|
|
|
struct cmd_device_list_visitor_context {
|
2023-04-13 18:59:37 +02:00
|
|
|
const struct shell *sh;
|
2021-02-22 11:42:50 -06:00
|
|
|
char *buf;
|
|
|
|
size_t buf_size;
|
|
|
|
};
|
|
|
|
|
|
|
|
static int cmd_device_list_visitor(const struct device *dev,
|
|
|
|
void *context)
|
|
|
|
{
|
|
|
|
const struct cmd_device_list_visitor_context *ctx = context;
|
|
|
|
|
2023-04-13 18:59:37 +02:00
|
|
|
shell_fprintf(ctx->sh, SHELL_NORMAL, " requires: %s\n",
|
2021-02-22 11:42:50 -06:00
|
|
|
get_device_name(dev, ctx->buf, ctx->buf_size));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-06-14 14:30:41 +02:00
|
|
|
#endif /* CONFIG_DEVICE_DEPS */
|
2021-02-22 11:42:50 -06:00
|
|
|
|
2022-10-04 15:05:12 +02:00
|
|
|
static int cmd_device_list(const struct shell *sh,
|
2021-02-22 11:42:50 -06:00
|
|
|
size_t argc, char **argv)
|
2018-10-05 09:38:55 -05:00
|
|
|
{
|
2020-12-08 09:03:57 -06:00
|
|
|
const struct device *devlist;
|
|
|
|
size_t devcnt = z_device_get_all_static(&devlist);
|
|
|
|
const struct device *devlist_end = devlist + devcnt;
|
2020-04-30 20:33:38 +02:00
|
|
|
const struct device *dev;
|
2023-03-21 12:10:19 -05:00
|
|
|
|
2018-10-05 09:38:55 -05:00
|
|
|
ARG_UNUSED(argc);
|
|
|
|
ARG_UNUSED(argv);
|
|
|
|
|
2022-10-04 15:05:12 +02:00
|
|
|
shell_fprintf(sh, SHELL_NORMAL, "devices:\n");
|
2020-03-09 11:02:20 +01:00
|
|
|
|
2020-12-08 09:03:57 -06:00
|
|
|
for (dev = devlist; dev < devlist_end; dev++) {
|
|
|
|
char buf[20];
|
|
|
|
const char *name = get_device_name(dev, buf, sizeof(buf));
|
|
|
|
const char *state = "READY";
|
2024-04-23 17:48:14 +01:00
|
|
|
int usage;
|
2020-12-08 09:03:57 -06:00
|
|
|
|
2022-10-04 15:05:12 +02:00
|
|
|
shell_fprintf(sh, SHELL_NORMAL, "- %s", name);
|
2020-11-30 12:18:44 -06:00
|
|
|
if (!device_is_ready(dev)) {
|
2020-12-08 09:03:57 -06:00
|
|
|
state = "DISABLED";
|
|
|
|
} else {
|
|
|
|
#ifdef CONFIG_PM_DEVICE
|
2021-06-03 19:06:53 +02:00
|
|
|
enum pm_device_state st = PM_DEVICE_STATE_ACTIVE;
|
2021-05-03 18:26:29 +02:00
|
|
|
int err = pm_device_state_get(dev, &st);
|
2020-03-09 11:02:20 +01:00
|
|
|
|
2020-12-08 09:03:57 -06:00
|
|
|
if (!err) {
|
2021-05-03 17:45:25 +02:00
|
|
|
state = pm_device_state_str(st);
|
2020-12-08 09:03:57 -06:00
|
|
|
}
|
|
|
|
#endif /* CONFIG_PM_DEVICE */
|
|
|
|
}
|
2020-03-09 11:02:20 +01:00
|
|
|
|
2024-04-23 17:48:14 +01:00
|
|
|
usage = pm_device_runtime_usage(dev);
|
|
|
|
if (usage >= 0) {
|
|
|
|
shell_fprintf(sh, SHELL_NORMAL, " (%s, usage=%d)\n", state, usage);
|
|
|
|
} else {
|
|
|
|
shell_fprintf(sh, SHELL_NORMAL, " (%s)\n", state);
|
|
|
|
}
|
|
|
|
|
2023-06-14 14:30:41 +02:00
|
|
|
#ifdef CONFIG_DEVICE_DEPS
|
2021-03-27 12:03:18 -04:00
|
|
|
if (!k_is_user_context()) {
|
2021-02-22 11:42:50 -06:00
|
|
|
struct cmd_device_list_visitor_context ctx = {
|
2023-04-13 18:59:37 +02:00
|
|
|
.sh = sh,
|
2021-02-22 11:42:50 -06:00
|
|
|
.buf = buf,
|
|
|
|
.buf_size = sizeof(buf),
|
|
|
|
};
|
|
|
|
|
|
|
|
(void)device_required_foreach(dev, cmd_device_list_visitor, &ctx);
|
2018-10-05 09:38:55 -05:00
|
|
|
}
|
2023-06-14 14:30:41 +02:00
|
|
|
#endif /* CONFIG_DEVICE_DEPS */
|
2024-04-25 02:05:58 -06:00
|
|
|
|
|
|
|
#ifdef CONFIG_DEVICE_DT_METADATA
|
|
|
|
const struct device_dt_nodelabels *nl = device_get_dt_nodelabels(dev);
|
|
|
|
|
2024-08-08 13:47:46 +02:00
|
|
|
if (nl != NULL && nl->num_nodelabels > 0) {
|
2024-04-25 02:05:58 -06:00
|
|
|
shell_fprintf(sh, SHELL_NORMAL, " DT node labels:");
|
|
|
|
for (size_t j = 0; j < nl->num_nodelabels; j++) {
|
|
|
|
const char *nodelabel = nl->nodelabels[j];
|
|
|
|
|
|
|
|
shell_fprintf(sh, SHELL_NORMAL, " %s", nodelabel);
|
|
|
|
}
|
|
|
|
shell_fprintf(sh, SHELL_NORMAL, "\n");
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_DEVICE_DT_METADATAa */
|
2018-10-05 09:38:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-03-21 12:10:19 -05:00
|
|
|
#ifdef CONFIG_PM_DEVICE_RUNTIME
|
|
|
|
static int cmd_device_pm_toggle(const struct shell *sh,
|
|
|
|
size_t argc, char **argv)
|
|
|
|
{
|
|
|
|
const struct device *dev;
|
|
|
|
enum pm_device_state pm_state;
|
|
|
|
|
|
|
|
dev = device_get_binding(argv[1]);
|
|
|
|
if (dev == NULL) {
|
|
|
|
shell_error(sh, "Device unknown (%s)", argv[1]);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!pm_device_runtime_is_enabled(dev)) {
|
|
|
|
shell_error(sh, "Device (%s) does not have runtime power management",
|
|
|
|
argv[1]);
|
|
|
|
return -ENOTSUP;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void)pm_device_state_get(dev, &pm_state);
|
|
|
|
|
|
|
|
if (pm_state == PM_DEVICE_STATE_ACTIVE) {
|
|
|
|
shell_fprintf(sh, SHELL_NORMAL, "pm_device_runtime_put(%s)\n",
|
|
|
|
argv[1]);
|
|
|
|
pm_device_runtime_put(dev);
|
|
|
|
} else {
|
|
|
|
shell_fprintf(sh, SHELL_NORMAL, "pm_device_runtime_get(%s)\n",
|
|
|
|
argv[1]);
|
|
|
|
pm_device_runtime_get(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#define PM_SHELL_CMD SHELL_CMD(pm_toggle, NULL, "Toggle device power (pm get/put)",\
|
|
|
|
cmd_device_pm_toggle),
|
|
|
|
#else
|
|
|
|
#define PM_SHELL_CMD
|
|
|
|
#endif /* CONFIG_PM_DEVICE_RUNTIME */
|
|
|
|
|
|
|
|
|
2018-10-05 09:38:55 -05:00
|
|
|
|
2019-02-13 14:53:29 +01:00
|
|
|
SHELL_STATIC_SUBCMD_SET_CREATE(sub_device,
|
2018-10-05 09:38:55 -05:00
|
|
|
SHELL_CMD(list, NULL, "List configured devices", cmd_device_list),
|
2023-03-21 12:10:19 -05:00
|
|
|
PM_SHELL_CMD
|
2018-10-05 09:38:55 -05:00
|
|
|
SHELL_SUBCMD_SET_END /* Array terminated. */
|
2019-02-13 14:53:29 +01:00
|
|
|
);
|
2018-10-05 09:38:55 -05:00
|
|
|
|
|
|
|
SHELL_CMD_REGISTER(device, &sub_device, "Device commands", NULL);
|