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"
87 " devctl getpath locator device\n"
93 devctl_table_handler(struct devctl_command **start,
94 struct devctl_command **end, int ac, char **av)
96 struct devctl_command **cmd;
99 warnx("The %s command requires a sub-command.", av[0]);
102 for (cmd = start; cmd < end; cmd++) {
103 if (strcmp((*cmd)->name, av[1]) == 0)
104 return ((*cmd)->handler(ac - 1, av + 1));
107 warnx("%s is not a valid sub-command of %s.", av[1], av[0]);
112 help(int ac __unused, char **av __unused)
118 DEVCTL_COMMAND(top, help, help);
121 attach(int ac, char **av)
126 if (devctl_attach(av[1]) < 0)
127 err(1, "Failed to attach %s", av[1]);
130 DEVCTL_COMMAND(top, attach, attach);
136 fprintf(stderr, "usage: devctl detach [-f] device\n");
141 detach(int ac, char **av)
147 while ((ch = getopt(ac, av, "f")) != -1)
160 if (devctl_detach(av[0], force) < 0)
161 err(1, "Failed to detach %s", av[0]);
164 DEVCTL_COMMAND(top, detach, detach);
170 fprintf(stderr, "usage: devctl disable [-f] device\n");
175 disable(int ac, char **av)
181 while ((ch = getopt(ac, av, "f")) != -1)
194 if (devctl_disable(av[0], force) < 0)
195 err(1, "Failed to disable %s", av[0]);
198 DEVCTL_COMMAND(top, disable, disable);
201 enable(int ac, char **av)
206 if (devctl_enable(av[1]) < 0)
207 err(1, "Failed to enable %s", av[1]);
210 DEVCTL_COMMAND(top, enable, enable);
213 suspend(int ac, char **av)
218 if (devctl_suspend(av[1]) < 0)
219 err(1, "Failed to suspend %s", av[1]);
222 DEVCTL_COMMAND(top, suspend, suspend);
225 resume(int ac, char **av)
230 if (devctl_resume(av[1]) < 0)
231 err(1, "Failed to resume %s", av[1]);
234 DEVCTL_COMMAND(top, resume, resume);
237 set_driver_usage(void)
240 fprintf(stderr, "usage: devctl set driver [-f] device driver\n");
245 set_driver(int ac, char **av)
251 while ((ch = getopt(ac, av, "f")) != -1)
264 if (devctl_set_driver(av[0], av[1], force) < 0)
265 err(1, "Failed to set %s driver to %s", av[0], av[1]);
268 DEVCTL_COMMAND(set, driver, set_driver);
271 clear_driver_usage(void)
274 fprintf(stderr, "usage: devctl clear driver [-f] device\n");
279 clear_driver(int ac, char **av)
285 while ((ch = getopt(ac, av, "f")) != -1)
291 clear_driver_usage();
297 clear_driver_usage();
298 if (devctl_clear_driver(av[0], force) < 0)
299 err(1, "Failed to clear %s driver", av[0]);
302 DEVCTL_COMMAND(clear, driver, clear_driver);
305 rescan(int ac, char **av)
310 if (devctl_rescan(av[1]) < 0)
311 err(1, "Failed to rescan %s", av[1]);
314 DEVCTL_COMMAND(top, rescan, rescan);
320 fprintf(stderr, "usage: devctl delete [-f] device\n");
325 delete(int ac, char **av)
331 while ((ch = getopt(ac, av, "f")) != -1)
344 if (devctl_delete(av[0], force) < 0)
345 err(1, "Failed to delete %s", av[0]);
348 DEVCTL_COMMAND(top, delete, delete);
354 fprintf(stderr, "usage: devctl freeze\n");
359 freeze(int ac, char **av __unused)
364 if (devctl_freeze() < 0)
365 err(1, "Failed to freeze probe/attach");
368 DEVCTL_COMMAND(top, freeze, freeze);
374 fprintf(stderr, "usage: devctl thaw\n");
379 thaw(int ac, char **av __unused)
384 if (devctl_thaw() < 0)
385 err(1, "Failed to thaw probe/attach");
388 DEVCTL_COMMAND(top, thaw, thaw);
394 fprintf(stderr, "usage: devctl reset [-d] device\n");
399 reset(int ac, char **av)
405 while ((ch = getopt(ac, av, "d")) != -1)
418 if (devctl_reset(av[0], detach_drv) < 0)
419 err(1, "Failed to reset %s", av[0]);
422 DEVCTL_COMMAND(top, reset, reset);
425 getpath(int ac, char **av)
431 if (devctl_getpath(av[2], av[1], &buffer) < 0)
432 err(1, "Failed to get path via %s to %s", av[1], av[2]);
433 printf("%s\n", buffer);
437 DEVCTL_COMMAND(top, getpath, getpath);
440 main(int ac, char *av[])
442 struct devctl_command **cmd;
449 SET_FOREACH(cmd, DEVCTL_DATASET(top)) {
450 if (strcmp((*cmd)->name, av[0]) == 0) {
451 if ((*cmd)->handler(ac, av) != 0)
457 warnx("Unknown command %s.", av[0]);