2 * Copyright (c) 2014 John Baldwin <jhb@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/linker_set.h>
39 struct devctl_command {
41 int (*handler)(int ac, char **av);
44 #define DEVCTL_DATASET(name) devctl_ ## name ## _table
46 #define DEVCTL_COMMAND(set, name, function) \
47 static struct devctl_command function ## _devctl_command = \
48 { #name, function }; \
49 DATA_SET(DEVCTL_DATASET(set), function ## _devctl_command)
51 #define DEVCTL_TABLE(set, name) \
52 SET_DECLARE(DEVCTL_DATASET(name), struct devctl_command); \
55 devctl_ ## name ## _table_handler(int ac, char **av) \
57 return (devctl_table_handler(SET_BEGIN(DEVCTL_DATASET(name)), \
58 SET_LIMIT(DEVCTL_DATASET(name)), ac, av)); \
60 DEVCTL_COMMAND(set, name, devctl_ ## name ## _table_handler)
62 static int devctl_table_handler(struct devctl_command **start,
63 struct devctl_command **end, int ac, char **av);
65 SET_DECLARE(DEVCTL_DATASET(top), struct devctl_command);
67 DEVCTL_TABLE(top, clear);
68 DEVCTL_TABLE(top, set);
74 "usage: devctl attach device\n"
75 " devctl detach [-f] device\n"
76 " devctl disable [-f] device\n"
77 " devctl enable device\n"
78 " devctl suspend device\n"
79 " devctl resume device\n"
80 " devctl set driver [-f] device driver\n"
81 " devctl clear driver [-f] device\n"
82 " devctl rescan device\n"
83 " devctl delete [-f] device\n"
86 " devctl reset [-d] device\n"
92 devctl_table_handler(struct devctl_command **start,
93 struct devctl_command **end, int ac, char **av)
95 struct devctl_command **cmd;
98 warnx("The %s command requires a sub-command.", av[0]);
101 for (cmd = start; cmd < end; cmd++) {
102 if (strcmp((*cmd)->name, av[1]) == 0)
103 return ((*cmd)->handler(ac - 1, av + 1));
106 warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
111 help(int ac __unused, char **av __unused)
117 DEVCTL_COMMAND(top, help, help);
120 attach(int ac, char **av)
125 if (devctl_attach(av[1]) < 0)
126 err(1, "Failed to attach %s", av[1]);
129 DEVCTL_COMMAND(top, attach, attach);
135 fprintf(stderr, "usage: devctl detach [-f] device\n");
140 detach(int ac, char **av)
146 while ((ch = getopt(ac, av, "f")) != -1)
159 if (devctl_detach(av[0], force) < 0)
160 err(1, "Failed to detach %s", av[0]);
163 DEVCTL_COMMAND(top, detach, detach);
169 fprintf(stderr, "usage: devctl disable [-f] device\n");
174 disable(int ac, char **av)
180 while ((ch = getopt(ac, av, "f")) != -1)
193 if (devctl_disable(av[0], force) < 0)
194 err(1, "Failed to disable %s", av[0]);
197 DEVCTL_COMMAND(top, disable, disable);
200 enable(int ac, char **av)
205 if (devctl_enable(av[1]) < 0)
206 err(1, "Failed to enable %s", av[1]);
209 DEVCTL_COMMAND(top, enable, enable);
212 suspend(int ac, char **av)
217 if (devctl_suspend(av[1]) < 0)
218 err(1, "Failed to suspend %s", av[1]);
221 DEVCTL_COMMAND(top, suspend, suspend);
224 resume(int ac, char **av)
229 if (devctl_resume(av[1]) < 0)
230 err(1, "Failed to resume %s", av[1]);
233 DEVCTL_COMMAND(top, resume, resume);
236 set_driver_usage(void)
239 fprintf(stderr, "usage: devctl set driver [-f] device driver\n");
244 set_driver(int ac, char **av)
250 while ((ch = getopt(ac, av, "f")) != -1)
263 if (devctl_set_driver(av[0], av[1], force) < 0)
264 err(1, "Failed to set %s driver to %s", av[0], av[1]);
267 DEVCTL_COMMAND(set, driver, set_driver);
270 clear_driver_usage(void)
273 fprintf(stderr, "usage: devctl clear driver [-f] device\n");
278 clear_driver(int ac, char **av)
284 while ((ch = getopt(ac, av, "f")) != -1)
290 clear_driver_usage();
296 clear_driver_usage();
297 if (devctl_clear_driver(av[0], force) < 0)
298 err(1, "Failed to clear %s driver", av[0]);
301 DEVCTL_COMMAND(clear, driver, clear_driver);
304 rescan(int ac, char **av)
309 if (devctl_rescan(av[1]) < 0)
310 err(1, "Failed to rescan %s", av[1]);
313 DEVCTL_COMMAND(top, rescan, rescan);
319 fprintf(stderr, "usage: devctl delete [-f] device\n");
324 delete(int ac, char **av)
330 while ((ch = getopt(ac, av, "f")) != -1)
343 if (devctl_delete(av[0], force) < 0)
344 err(1, "Failed to delete %s", av[0]);
347 DEVCTL_COMMAND(top, delete, delete);
353 fprintf(stderr, "usage: devctl freeze\n");
358 freeze(int ac, char **av __unused)
363 if (devctl_freeze() < 0)
364 err(1, "Failed to freeze probe/attach");
367 DEVCTL_COMMAND(top, freeze, freeze);
373 fprintf(stderr, "usage: devctl thaw\n");
378 thaw(int ac, char **av __unused)
383 if (devctl_thaw() < 0)
384 err(1, "Failed to thaw probe/attach");
387 DEVCTL_COMMAND(top, thaw, thaw);
393 fprintf(stderr, "usage: devctl reset [-d] device\n");
398 reset(int ac, char **av)
404 while ((ch = getopt(ac, av, "d")) != -1)
417 if (devctl_reset(av[0], detach_drv) < 0)
418 err(1, "Failed to reset %s", av[0]);
421 DEVCTL_COMMAND(top, reset, reset);
424 main(int ac, char *av[])
426 struct devctl_command **cmd;
433 SET_FOREACH(cmd, DEVCTL_DATASET(top)) {
434 if (strcmp((*cmd)->name, av[0]) == 0) {
435 if ((*cmd)->handler(ac, av) != 0)
441 warnx("Unknown command %s.", av[0]);