2 * Copyright (c) 2014 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/linker_set.h>
40 struct devctl_command {
42 int (*handler)(int ac, char **av);
45 #define DEVCTL_DATASET(name) devctl_ ## name ## _table
47 #define DEVCTL_COMMAND(set, name, function) \
48 static struct devctl_command function ## _devctl_command = \
49 { #name, function }; \
50 DATA_SET(DEVCTL_DATASET(set), function ## _devctl_command)
52 #define DEVCTL_TABLE(set, name) \
53 SET_DECLARE(DEVCTL_DATASET(name), struct devctl_command); \
56 devctl_ ## name ## _table_handler(int ac, char **av) \
58 return (devctl_table_handler(SET_BEGIN(DEVCTL_DATASET(name)), \
59 SET_LIMIT(DEVCTL_DATASET(name)), ac, av)); \
61 DEVCTL_COMMAND(set, name, devctl_ ## name ## _table_handler)
63 static int devctl_table_handler(struct devctl_command **start,
64 struct devctl_command **end, int ac, char **av);
66 SET_DECLARE(DEVCTL_DATASET(top), struct devctl_command);
68 DEVCTL_TABLE(top, clear);
69 DEVCTL_TABLE(top, set);
74 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
75 "usage: devctl attach device",
76 " devctl detach [-f] device",
77 " devctl disable [-f] device",
78 " devctl enable device",
79 " devctl suspend device",
80 " devctl resume device",
81 " devctl set driver [-f] device driver",
82 " devctl clear driver [-f] device",
83 " devctl rescan device",
84 " devctl delete [-f] device");
89 devctl_table_handler(struct devctl_command **start,
90 struct devctl_command **end, int ac, char **av)
92 struct devctl_command **cmd;
95 warnx("The %s command requires a sub-command.", av[0]);
98 for (cmd = start; cmd < end; cmd++) {
99 if (strcmp((*cmd)->name, av[1]) == 0)
100 return ((*cmd)->handler(ac - 1, av + 1));
103 warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
108 help(int ac __unused, char **av __unused)
114 DEVCTL_COMMAND(top, help, help);
117 attach(int ac, char **av)
122 if (devctl_attach(av[1]) < 0)
123 err(1, "Failed to attach %s", av[1]);
126 DEVCTL_COMMAND(top, attach, attach);
132 fprintf(stderr, "usage: devctl detach [-f] device\n");
137 detach(int ac, char **av)
143 while ((ch = getopt(ac, av, "f")) != -1)
156 if (devctl_detach(av[0], force) < 0)
157 err(1, "Failed to detach %s", av[0]);
160 DEVCTL_COMMAND(top, detach, detach);
166 fprintf(stderr, "usage: devctl disable [-f] device\n");
171 disable(int ac, char **av)
177 while ((ch = getopt(ac, av, "f")) != -1)
190 if (devctl_disable(av[0], force) < 0)
191 err(1, "Failed to disable %s", av[0]);
194 DEVCTL_COMMAND(top, disable, disable);
197 enable(int ac, char **av)
202 if (devctl_enable(av[1]) < 0)
203 err(1, "Failed to enable %s", av[1]);
206 DEVCTL_COMMAND(top, enable, enable);
209 suspend(int ac, char **av)
214 if (devctl_suspend(av[1]) < 0)
215 err(1, "Failed to suspend %s", av[1]);
218 DEVCTL_COMMAND(top, suspend, suspend);
221 resume(int ac, char **av)
226 if (devctl_resume(av[1]) < 0)
227 err(1, "Failed to resume %s", av[1]);
230 DEVCTL_COMMAND(top, resume, resume);
233 set_driver_usage(void)
236 fprintf(stderr, "usage: devctl set driver [-f] device driver\n");
241 set_driver(int ac, char **av)
247 while ((ch = getopt(ac, av, "f")) != -1)
260 if (devctl_set_driver(av[0], av[1], force) < 0)
261 err(1, "Failed to set %s driver to %s", av[0], av[1]);
264 DEVCTL_COMMAND(set, driver, set_driver);
267 clear_driver_usage(void)
270 fprintf(stderr, "usage: devctl clear driver [-f] device\n");
275 clear_driver(int ac, char **av)
281 while ((ch = getopt(ac, av, "f")) != -1)
287 clear_driver_usage();
293 clear_driver_usage();
294 if (devctl_clear_driver(av[0], force) < 0)
295 err(1, "Failed to clear %s driver", av[0]);
298 DEVCTL_COMMAND(clear, driver, clear_driver);
301 rescan(int ac, char **av)
306 if (devctl_rescan(av[1]) < 0)
307 err(1, "Failed to rescan %s", av[1]);
310 DEVCTL_COMMAND(top, rescan, rescan);
316 fprintf(stderr, "usage: devctl delete [-f] device\n");
321 delete(int ac, char **av)
327 while ((ch = getopt(ac, av, "f")) != -1)
340 if (devctl_delete(av[0], force) < 0)
341 err(1, "Failed to delete %s", av[0]);
344 DEVCTL_COMMAND(top, delete, delete);
347 main(int ac, char *av[])
349 struct devctl_command **cmd;
356 SET_FOREACH(cmd, DEVCTL_DATASET(top)) {
357 if (strcmp((*cmd)->name, av[0]) == 0) {
358 if ((*cmd)->handler(ac, av) != 0)
364 warnx("Unknown command %s.", av[0]);