1 /* $NetBSD: drvctl.c,v 1.9 2009/04/20 21:40:42 dyoung Exp $ */
5 * Matthias Drochner. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include <sys/ioctl.h>
37 #include <sys/drvctlio.h>
39 #define OPTS "QRSa:dlnpr"
41 #define OPEN_MODE(mode) \
42 (((mode) == 'd' || (mode) == 'r') ? O_RDWR \
45 static void usage(void);
51 fprintf(stderr
, "Usage: %s -r [-a attribute] busdevice [locator ...]\n"
53 " %s [-n] -l [device]\n"
58 getprogname(), getprogname(), getprogname(), getprogname(),
59 getprogname(), getprogname(), getprogname());
64 main(int argc
, char **argv
)
73 struct devpmargs paa
= {.devname
= "", .flags
= 0};
74 struct devlistargs laa
= {.l_devname
= "", .l_childname
= NULL
,
76 struct devdetachargs daa
;
77 struct devrescanargs raa
;
79 prop_dictionary_t command_dict
, args_dict
, results_dict
,
86 while ((c
= getopt(argc
, argv
, OPTS
)) != -1) {
112 if ((argc
< 1 && mode
!= 'l') || mode
== 0)
115 fd
= open(DRVCTLDEV
, OPEN_MODE(mode
), 0);
117 err(2, "open %s", DRVCTLDEV
);
121 paa
.flags
= DEVPM_F_SUBTREE
;
124 strlcpy(paa
.devname
, argv
[0], sizeof(paa
.devname
));
126 if (ioctl(fd
, DRVRESUMEDEV
, &paa
) == -1)
127 err(3, "DRVRESUMEDEV");
130 strlcpy(paa
.devname
, argv
[0], sizeof(paa
.devname
));
132 if (ioctl(fd
, DRVSUSPENDDEV
, &paa
) == -1)
133 err(3, "DRVSUSPENDDEV");
136 strlcpy(daa
.devname
, argv
[0], sizeof(daa
.devname
));
138 if (ioctl(fd
, DRVDETACHDEV
, &daa
) == -1)
139 err(3, "DRVDETACHDEV");
143 *laa
.l_devname
= '\0';
145 strlcpy(laa
.l_devname
, argv
[0], sizeof(laa
.l_devname
));
147 if (ioctl(fd
, DRVLISTDEV
, &laa
) == -1)
148 err(3, "DRVLISTDEV");
150 children
= laa
.l_children
;
152 laa
.l_childname
= malloc(children
* sizeof(laa
.l_childname
[0]));
153 if (laa
.l_childname
== NULL
)
154 err(5, "DRVLISTDEV");
155 if (ioctl(fd
, DRVLISTDEV
, &laa
) == -1)
156 err(3, "DRVLISTDEV");
157 if (laa
.l_children
> children
)
158 err(6, "DRVLISTDEV: number of children grew");
160 for (i
= 0; i
< (int)laa
.l_children
; i
++) {
163 (argc
== 0) ? "root" : laa
.l_devname
);
165 printf("%s\n", laa
.l_childname
[i
]);
169 memset(&raa
, 0, sizeof(raa
));
170 strlcpy(raa
.busname
, argv
[0], sizeof(raa
.busname
));
172 strlcpy(raa
.ifattr
, attr
, sizeof(raa
.ifattr
));
174 locs
= malloc((argc
- 1) * sizeof(int));
176 err(5, "malloc int[%d]", argc
- 1);
177 for (i
= 0; i
< argc
- 1; i
++)
178 locs
[i
] = atoi(argv
[i
+ 1]);
179 raa
.numlocators
= argc
- 1;
183 if (ioctl(fd
, DRVRESCANBUS
, &raa
) == -1)
184 err(3, "DRVRESCANBUS");
188 command_dict
= prop_dictionary_create();
189 args_dict
= prop_dictionary_create();
191 string
= prop_string_create_cstring_nocopy("get-properties");
192 prop_dictionary_set(command_dict
, "drvctl-command", string
);
193 prop_object_release(string
);
195 string
= prop_string_create_cstring(argv
[0]);
196 prop_dictionary_set(args_dict
, "device-name", string
);
197 prop_object_release(string
);
199 prop_dictionary_set(command_dict
, "drvctl-arguments",
201 prop_object_release(args_dict
);
203 res
= prop_dictionary_sendrecv_ioctl(command_dict
, fd
,
206 prop_object_release(command_dict
);
208 errx(3, "DRVCTLCOMMAND: %s", strerror(res
));
210 number
= prop_dictionary_get(results_dict
, "drvctl-error");
211 if (prop_number_integer_value(number
) != 0) {
212 errx(3, "get-properties: %s",
213 strerror((int)prop_number_integer_value(number
)));
216 data_dict
= prop_dictionary_get(results_dict
,
217 "drvctl-result-data");
218 if (data_dict
== NULL
) {
219 errx(3, "get-properties: failed to return result data");
222 xml
= prop_dictionary_externalize(data_dict
);
223 prop_object_release(results_dict
);
225 printf("Properties for device `%s':\n%s",
230 errx(4, "unknown command");