kern_drvctl.c revision 1.14 1 1.14 joerg /* $NetBSD: kern_drvctl.c,v 1.14 2008/02/12 17:30:59 joerg Exp $ */
2 1.1 drochner
3 1.1 drochner /*
4 1.1 drochner * Copyright (c) 2004
5 1.1 drochner * Matthias Drochner. All rights reserved.
6 1.1 drochner *
7 1.1 drochner * Redistribution and use in source and binary forms, with or without
8 1.1 drochner * modification, are permitted provided that the following conditions
9 1.1 drochner * are met:
10 1.1 drochner * 1. Redistributions of source code must retain the above copyright
11 1.1 drochner * notice, this list of conditions, and the following disclaimer.
12 1.1 drochner * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 drochner * notice, this list of conditions and the following disclaimer in the
14 1.1 drochner * documentation and/or other materials provided with the distribution.
15 1.1 drochner *
16 1.1 drochner * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 drochner * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 drochner * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 drochner * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 drochner * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 drochner * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 drochner * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 drochner * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 drochner * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 drochner * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 drochner * SUCH DAMAGE.
27 1.1 drochner */
28 1.1 drochner
29 1.1 drochner #include <sys/cdefs.h>
30 1.14 joerg __KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.14 2008/02/12 17:30:59 joerg Exp $");
31 1.1 drochner
32 1.1 drochner #include <sys/param.h>
33 1.1 drochner #include <sys/systm.h>
34 1.1 drochner #include <sys/kernel.h>
35 1.1 drochner #include <sys/conf.h>
36 1.1 drochner #include <sys/device.h>
37 1.1 drochner #include <sys/event.h>
38 1.1 drochner #include <sys/malloc.h>
39 1.1 drochner #include <sys/ioctl.h>
40 1.5 thorpej #include <sys/fcntl.h>
41 1.1 drochner #include <sys/drvctlio.h>
42 1.1 drochner
43 1.1 drochner dev_type_ioctl(drvctlioctl);
44 1.1 drochner
45 1.1 drochner const struct cdevsw drvctl_cdevsw = {
46 1.1 drochner nullopen, nullclose, nullread, nullwrite, drvctlioctl,
47 1.6 gdt nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
48 1.1 drochner };
49 1.1 drochner
50 1.1 drochner void drvctlattach(int);
51 1.1 drochner
52 1.1 drochner #define MAXLOCATORS 100
53 1.1 drochner
54 1.5 thorpej static int drvctl_command(struct lwp *, struct plistref *, u_long, int flag);
55 1.5 thorpej
56 1.12 dyoung static int
57 1.12 dyoung pmdevbyname(int cmd, struct devpmargs *a)
58 1.12 dyoung {
59 1.12 dyoung struct device *d;
60 1.12 dyoung
61 1.14 joerg if ((d = device_find_by_xname(a->devname)) == NULL)
62 1.12 dyoung return ENXIO;
63 1.12 dyoung
64 1.12 dyoung switch (cmd) {
65 1.12 dyoung case DRVSUSPENDDEV:
66 1.12 dyoung return pmf_device_recursive_suspend(d) ? 0 : EBUSY;
67 1.12 dyoung case DRVRESUMEDEV:
68 1.12 dyoung if (a->flags & DEVPM_F_SUBTREE)
69 1.12 dyoung return pmf_device_resume_subtree(d) ? 0 : EBUSY;
70 1.12 dyoung else
71 1.12 dyoung return pmf_device_recursive_resume(d) ? 0 : EBUSY;
72 1.12 dyoung default:
73 1.12 dyoung return EPASSTHROUGH;
74 1.12 dyoung }
75 1.12 dyoung }
76 1.12 dyoung
77 1.12 dyoung static int
78 1.12 dyoung listdevbyname(struct devlistargs *l)
79 1.12 dyoung {
80 1.13 drochner device_t d, child;
81 1.13 drochner int cnt = 0, idx, error;
82 1.12 dyoung
83 1.14 joerg if ((d = device_find_by_xname(l->l_devname)) == NULL)
84 1.12 dyoung return ENXIO;
85 1.12 dyoung
86 1.13 drochner TAILQ_FOREACH(child, &alldevs, dv_list) {
87 1.13 drochner if (device_parent(child) != d)
88 1.13 drochner continue;
89 1.13 drochner idx = cnt++;
90 1.13 drochner if (l->l_childname == NULL || idx >= l->l_children)
91 1.13 drochner continue;
92 1.13 drochner error = copyoutstr(device_xname(child), l->l_childname[idx],
93 1.13 drochner sizeof(l->l_childname[idx]), NULL);
94 1.13 drochner if (error)
95 1.13 drochner return error;
96 1.13 drochner }
97 1.12 dyoung
98 1.13 drochner l->l_children = cnt;
99 1.12 dyoung return 0;
100 1.12 dyoung }
101 1.12 dyoung
102 1.1 drochner static int
103 1.1 drochner detachdevbyname(const char *devname)
104 1.1 drochner {
105 1.1 drochner struct device *d;
106 1.1 drochner
107 1.14 joerg if ((d = device_find_by_xname(devname)) == NULL)
108 1.12 dyoung return ENXIO;
109 1.12 dyoung
110 1.1 drochner #ifndef XXXFULLRISK
111 1.12 dyoung /*
112 1.12 dyoung * If the parent cannot be notified, it might keep
113 1.12 dyoung * pointers to the detached device.
114 1.12 dyoung * There might be a private notification mechanism,
115 1.12 dyoung * but better play save here.
116 1.12 dyoung */
117 1.12 dyoung if (d->dv_parent && !d->dv_parent->dv_cfattach->ca_childdetached)
118 1.12 dyoung return (ENOTSUP);
119 1.1 drochner #endif
120 1.12 dyoung return (config_detach(d, 0));
121 1.1 drochner }
122 1.1 drochner
123 1.1 drochner static int
124 1.1 drochner rescanbus(const char *busname, const char *ifattr,
125 1.1 drochner int numlocators, const int *locators)
126 1.1 drochner {
127 1.12 dyoung int i, rc;
128 1.1 drochner struct device *d;
129 1.2 drochner const struct cfiattrdata * const *ap;
130 1.1 drochner
131 1.1 drochner /* XXX there should be a way to get limits and defaults (per device)
132 1.1 drochner from config generated data */
133 1.1 drochner int locs[MAXLOCATORS];
134 1.1 drochner for (i = 0; i < MAXLOCATORS; i++)
135 1.1 drochner locs[i] = -1;
136 1.1 drochner
137 1.1 drochner for (i = 0; i < numlocators;i++)
138 1.1 drochner locs[i] = locators[i];
139 1.1 drochner
140 1.14 joerg if ((d = device_find_by_xname(busname)) == NULL)
141 1.12 dyoung return ENXIO;
142 1.1 drochner
143 1.12 dyoung /*
144 1.12 dyoung * must support rescan, and must have something
145 1.12 dyoung * to attach to
146 1.12 dyoung */
147 1.12 dyoung if (!d->dv_cfattach->ca_rescan ||
148 1.12 dyoung !d->dv_cfdriver->cd_attrs)
149 1.12 dyoung return (ENODEV);
150 1.12 dyoung
151 1.12 dyoung /* allow to omit attribute if there is exactly one */
152 1.12 dyoung if (!ifattr) {
153 1.12 dyoung if (d->dv_cfdriver->cd_attrs[1])
154 1.12 dyoung return (EINVAL);
155 1.12 dyoung ifattr = d->dv_cfdriver->cd_attrs[0]->ci_name;
156 1.12 dyoung } else {
157 1.12 dyoung /* check for valid attribute passed */
158 1.12 dyoung for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++)
159 1.12 dyoung if (!strcmp((*ap)->ci_name, ifattr))
160 1.12 dyoung break;
161 1.12 dyoung if (!*ap)
162 1.12 dyoung return (EINVAL);
163 1.1 drochner }
164 1.1 drochner
165 1.12 dyoung rc = (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
166 1.12 dyoung config_deferred(NULL);
167 1.12 dyoung return rc;
168 1.1 drochner }
169 1.1 drochner
170 1.1 drochner int
171 1.10 christos drvctlioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *p)
172 1.1 drochner {
173 1.1 drochner int res;
174 1.1 drochner char *ifattr;
175 1.1 drochner int *locs;
176 1.1 drochner
177 1.1 drochner switch (cmd) {
178 1.12 dyoung case DRVSUSPENDDEV:
179 1.12 dyoung case DRVRESUMEDEV:
180 1.12 dyoung #define d ((struct devpmargs *)data)
181 1.12 dyoung res = pmdevbyname(cmd, d);
182 1.12 dyoung #undef d
183 1.12 dyoung break;
184 1.12 dyoung case DRVLISTDEV:
185 1.12 dyoung res = listdevbyname((struct devlistargs *)data);
186 1.12 dyoung break;
187 1.1 drochner case DRVDETACHDEV:
188 1.1 drochner #define d ((struct devdetachargs *)data)
189 1.1 drochner res = detachdevbyname(d->devname);
190 1.1 drochner #undef d
191 1.1 drochner break;
192 1.1 drochner case DRVRESCANBUS:
193 1.1 drochner #define d ((struct devrescanargs *)data)
194 1.1 drochner d->busname[sizeof(d->busname) - 1] = '\0';
195 1.1 drochner
196 1.1 drochner /* XXX better copyin? */
197 1.1 drochner if (d->ifattr[0]) {
198 1.1 drochner d->ifattr[sizeof(d->ifattr) - 1] = '\0';
199 1.1 drochner ifattr = d->ifattr;
200 1.1 drochner } else
201 1.1 drochner ifattr = 0;
202 1.1 drochner
203 1.1 drochner if (d->numlocators) {
204 1.1 drochner if (d->numlocators > MAXLOCATORS)
205 1.1 drochner return (EINVAL);
206 1.1 drochner locs = malloc(d->numlocators * sizeof(int), M_DEVBUF,
207 1.1 drochner M_WAITOK);
208 1.1 drochner res = copyin(d->locators, locs,
209 1.1 drochner d->numlocators * sizeof(int));
210 1.11 rmind if (res) {
211 1.11 rmind free(locs, M_DEVBUF);
212 1.1 drochner return (res);
213 1.11 rmind }
214 1.1 drochner } else
215 1.1 drochner locs = 0;
216 1.1 drochner res = rescanbus(d->busname, ifattr, d->numlocators, locs);
217 1.1 drochner if (locs)
218 1.1 drochner free(locs, M_DEVBUF);
219 1.1 drochner #undef d
220 1.5 thorpej break;
221 1.5 thorpej case DRVCTLCOMMAND:
222 1.5 thorpej res = drvctl_command(p, (struct plistref *)data, cmd, flag);
223 1.5 thorpej break;
224 1.5 thorpej default:
225 1.5 thorpej return (EPASSTHROUGH);
226 1.1 drochner }
227 1.1 drochner return (res);
228 1.1 drochner }
229 1.1 drochner
230 1.1 drochner void
231 1.9 yamt drvctlattach(int arg)
232 1.1 drochner {
233 1.1 drochner }
234 1.5 thorpej
235 1.5 thorpej /*****************************************************************************
236 1.5 thorpej * Driver control command processing engine
237 1.5 thorpej *****************************************************************************/
238 1.5 thorpej
239 1.5 thorpej static int
240 1.9 yamt drvctl_command_get_properties(struct lwp *l,
241 1.5 thorpej prop_dictionary_t command_dict,
242 1.5 thorpej prop_dictionary_t results_dict)
243 1.5 thorpej {
244 1.5 thorpej prop_dictionary_t args_dict;
245 1.5 thorpej prop_string_t devname_string;
246 1.5 thorpej device_t dev;
247 1.5 thorpej
248 1.5 thorpej args_dict = prop_dictionary_get(command_dict, "drvctl-arguments");
249 1.5 thorpej if (args_dict == NULL)
250 1.5 thorpej return (EINVAL);
251 1.5 thorpej
252 1.5 thorpej devname_string = prop_dictionary_get(args_dict, "device-name");
253 1.5 thorpej if (devname_string == NULL)
254 1.5 thorpej return (EINVAL);
255 1.5 thorpej
256 1.5 thorpej TAILQ_FOREACH(dev, &alldevs, dv_list) {
257 1.5 thorpej if (prop_string_equals_cstring(devname_string,
258 1.5 thorpej device_xname(dev)))
259 1.5 thorpej break;
260 1.5 thorpej }
261 1.5 thorpej
262 1.5 thorpej if (dev == NULL)
263 1.5 thorpej return (ESRCH);
264 1.5 thorpej
265 1.5 thorpej prop_dictionary_set(results_dict, "drvctl-result-data",
266 1.5 thorpej device_properties(dev));
267 1.5 thorpej
268 1.5 thorpej return (0);
269 1.5 thorpej }
270 1.5 thorpej
271 1.5 thorpej struct drvctl_command_desc {
272 1.5 thorpej const char *dcd_name; /* command name */
273 1.5 thorpej int (*dcd_func)(struct lwp *, /* handler function */
274 1.5 thorpej prop_dictionary_t,
275 1.5 thorpej prop_dictionary_t);
276 1.5 thorpej int dcd_rw; /* read or write required */
277 1.5 thorpej };
278 1.5 thorpej
279 1.5 thorpej static const struct drvctl_command_desc drvctl_command_table[] = {
280 1.5 thorpej { .dcd_name = "get-properties",
281 1.5 thorpej .dcd_func = drvctl_command_get_properties,
282 1.5 thorpej .dcd_rw = FREAD,
283 1.5 thorpej },
284 1.5 thorpej
285 1.5 thorpej { .dcd_name = NULL }
286 1.5 thorpej };
287 1.5 thorpej
288 1.5 thorpej static int
289 1.5 thorpej drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
290 1.5 thorpej int fflag)
291 1.5 thorpej {
292 1.5 thorpej prop_dictionary_t command_dict, results_dict;
293 1.5 thorpej prop_string_t command_string;
294 1.5 thorpej const struct drvctl_command_desc *dcd;
295 1.5 thorpej int error;
296 1.5 thorpej
297 1.5 thorpej error = prop_dictionary_copyin_ioctl(pref, ioctl_cmd, &command_dict);
298 1.5 thorpej if (error)
299 1.5 thorpej return (error);
300 1.5 thorpej
301 1.5 thorpej results_dict = prop_dictionary_create();
302 1.5 thorpej if (results_dict == NULL) {
303 1.5 thorpej prop_object_release(command_dict);
304 1.5 thorpej return (ENOMEM);
305 1.5 thorpej }
306 1.5 thorpej
307 1.5 thorpej command_string = prop_dictionary_get(command_dict, "drvctl-command");
308 1.5 thorpej if (command_string == NULL) {
309 1.5 thorpej error = EINVAL;
310 1.5 thorpej goto out;
311 1.5 thorpej }
312 1.5 thorpej
313 1.5 thorpej for (dcd = drvctl_command_table; dcd->dcd_name != NULL; dcd++) {
314 1.5 thorpej if (prop_string_equals_cstring(command_string,
315 1.5 thorpej dcd->dcd_name))
316 1.5 thorpej break;
317 1.5 thorpej }
318 1.5 thorpej
319 1.5 thorpej if (dcd->dcd_name == NULL) {
320 1.5 thorpej error = EINVAL;
321 1.5 thorpej goto out;
322 1.5 thorpej }
323 1.5 thorpej
324 1.5 thorpej if ((fflag & dcd->dcd_rw) == 0) {
325 1.5 thorpej error = EPERM;
326 1.5 thorpej goto out;
327 1.5 thorpej }
328 1.5 thorpej
329 1.5 thorpej error = (*dcd->dcd_func)(l, command_dict, results_dict);
330 1.5 thorpej
331 1.8 thorpej prop_dictionary_set_int32(results_dict, "drvctl-error", error);
332 1.5 thorpej
333 1.5 thorpej error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict);
334 1.5 thorpej out:
335 1.5 thorpej prop_object_release(command_dict);
336 1.5 thorpej prop_object_release(results_dict);
337 1.5 thorpej return (error);
338 1.5 thorpej }
339