summaryrefslogtreecommitdiffstats
path: root/commands/devunbind.c
blob: 3f9cd7b849c89918930b2403333a8db1db8747b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// SPDX-License-Identifier: GPL-2.0-only
// SPDX-FileCopyrightText: © 2021 Ahmad Fatoum <a.fatoum@pengutronix.de>, Pengutronix

#include <command.h>
#include <common.h>
#include <complete.h>
#include <driver.h>
#include <getopt.h>

static int do_devunbind(int argc, char *argv[])
{
	bool unregister = false;
	struct device_d *dev;
	int ret = COMMAND_SUCCESS, i, opt;

	while ((opt = getopt(argc, argv, "fl")) > 0) {
		switch (opt) {
		case 'f':
			unregister = true;
			break;
		case 'l':
			list_for_each_entry(dev, &active_device_list, active) {
				BUG_ON(!dev->driver);
				if (dev->bus->remove)
					printf("%pS(%s, %s)\n", dev->bus->remove,
					       dev->driver->name, dev_name(dev));
			}
			return 0;
		default:
			return COMMAND_ERROR_USAGE;
		}
	}

	if (!argv[optind])
		return COMMAND_ERROR_USAGE;

	for (i = optind; i < argc; i++) {
		dev = get_device_by_name(argv[i]);
		if (!dev) {
			printf("skipping missing %s\n", argv[i]);
			ret = -ENODEV;
			continue;
		}

		if (unregister) {
			unregister_device(dev);
			continue;
		}

		if (!dev->driver || !dev->bus->remove) {
			printf("skipping unbound %s\n", argv[i]);
			ret = COMMAND_ERROR;
			continue;
		}

		dev->bus->remove(dev);
		dev->driver = NULL;
		list_del(&dev->active);
	}

	return ret;
}

BAREBOX_CMD_HELP_START(devunbind)
BAREBOX_CMD_HELP_TEXT("Debugging aid to unbind device from driver at runtime")
BAREBOX_CMD_HELP_TEXT("")
BAREBOX_CMD_HELP_TEXT("Options:")
BAREBOX_CMD_HELP_OPT ("-f",  "unbind driver and force removal of device and children")
BAREBOX_CMD_HELP_OPT ("-l",  "list remove callbacks in shutdown order")
BAREBOX_CMD_HELP_END

BAREBOX_CMD_START(devunbind)
	.cmd		= do_devunbind,
	BAREBOX_CMD_DESC("unbind device(s) from driver")
	BAREBOX_CMD_OPTS("[-fl] DEVICES..")
	BAREBOX_CMD_GROUP(CMD_GRP_INFO)
	BAREBOX_CMD_HELP(cmd_devunbind_help)
	BAREBOX_CMD_COMPLETE(device_complete)
BAREBOX_CMD_END