kern_drvctl.c revision 1.31 1 1.31 dsl /* $NetBSD: kern_drvctl.c,v 1.31 2009/12/20 09:36:05 dsl 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.31 dsl __KERNEL_RCSID(0, "$NetBSD: kern_drvctl.c,v 1.31 2009/12/20 09:36:05 dsl 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.17 jmcneill #include <sys/kmem.h>
39 1.1 drochner #include <sys/ioctl.h>
40 1.5 thorpej #include <sys/fcntl.h>
41 1.17 jmcneill #include <sys/file.h>
42 1.17 jmcneill #include <sys/filedesc.h>
43 1.20 jmcneill #include <sys/select.h>
44 1.20 jmcneill #include <sys/poll.h>
45 1.1 drochner #include <sys/drvctlio.h>
46 1.17 jmcneill #include <sys/devmon.h>
47 1.25 christos #include <sys/stat.h>
48 1.26 christos #include <sys/kauth.h>
49 1.27 nonaka #include <sys/lwp.h>
50 1.1 drochner
51 1.17 jmcneill struct drvctl_event {
52 1.17 jmcneill TAILQ_ENTRY(drvctl_event) dce_link;
53 1.17 jmcneill prop_dictionary_t dce_event;
54 1.17 jmcneill };
55 1.17 jmcneill
56 1.17 jmcneill TAILQ_HEAD(drvctl_queue, drvctl_event);
57 1.17 jmcneill
58 1.17 jmcneill static struct drvctl_queue drvctl_eventq; /* FIFO */
59 1.17 jmcneill static kcondvar_t drvctl_cond;
60 1.17 jmcneill static kmutex_t drvctl_lock;
61 1.17 jmcneill static int drvctl_nopen = 0, drvctl_eventcnt = 0;
62 1.20 jmcneill static struct selinfo drvctl_rdsel;
63 1.17 jmcneill
64 1.17 jmcneill #define DRVCTL_EVENTQ_DEPTH 64 /* arbitrary queue limit */
65 1.17 jmcneill
66 1.17 jmcneill dev_type_open(drvctlopen);
67 1.1 drochner
68 1.1 drochner const struct cdevsw drvctl_cdevsw = {
69 1.17 jmcneill drvctlopen, nullclose, nullread, nullwrite, noioctl,
70 1.6 gdt nostop, notty, nopoll, nommap, nokqfilter, D_OTHER
71 1.1 drochner };
72 1.1 drochner
73 1.1 drochner void drvctlattach(int);
74 1.1 drochner
75 1.17 jmcneill static int drvctl_read(struct file *, off_t *, struct uio *,
76 1.17 jmcneill kauth_cred_t, int);
77 1.17 jmcneill static int drvctl_write(struct file *, off_t *, struct uio *,
78 1.17 jmcneill kauth_cred_t, int);
79 1.17 jmcneill static int drvctl_ioctl(struct file *, u_long, void *);
80 1.20 jmcneill static int drvctl_poll(struct file *, int);
81 1.25 christos static int drvctl_stat(struct file *, struct stat *);
82 1.17 jmcneill static int drvctl_close(struct file *);
83 1.17 jmcneill
84 1.17 jmcneill static const struct fileops drvctl_fileops = {
85 1.23 ad .fo_read = drvctl_read,
86 1.23 ad .fo_write = drvctl_write,
87 1.23 ad .fo_ioctl = drvctl_ioctl,
88 1.23 ad .fo_fcntl = fnullop_fcntl,
89 1.23 ad .fo_poll = drvctl_poll,
90 1.25 christos .fo_stat = drvctl_stat,
91 1.23 ad .fo_close = drvctl_close,
92 1.23 ad .fo_kqfilter = fnullop_kqfilter,
93 1.31 dsl .fo_restart = fnullop_restart,
94 1.17 jmcneill };
95 1.17 jmcneill
96 1.1 drochner #define MAXLOCATORS 100
97 1.1 drochner
98 1.17 jmcneill static int drvctl_command(struct lwp *, struct plistref *, u_long, int);
99 1.17 jmcneill static int drvctl_getevent(struct lwp *, struct plistref *, u_long, int);
100 1.17 jmcneill
101 1.17 jmcneill void
102 1.17 jmcneill drvctl_init(void)
103 1.17 jmcneill {
104 1.17 jmcneill TAILQ_INIT(&drvctl_eventq);
105 1.17 jmcneill mutex_init(&drvctl_lock, MUTEX_DEFAULT, IPL_NONE);
106 1.17 jmcneill cv_init(&drvctl_cond, "devmon");
107 1.20 jmcneill selinit(&drvctl_rdsel);
108 1.17 jmcneill }
109 1.17 jmcneill
110 1.17 jmcneill void
111 1.17 jmcneill devmon_insert(const char *event, prop_dictionary_t ev)
112 1.17 jmcneill {
113 1.21 yamt struct drvctl_event *dce, *odce;
114 1.17 jmcneill
115 1.17 jmcneill mutex_enter(&drvctl_lock);
116 1.17 jmcneill
117 1.17 jmcneill if (drvctl_nopen == 0) {
118 1.17 jmcneill mutex_exit(&drvctl_lock);
119 1.17 jmcneill return;
120 1.17 jmcneill }
121 1.17 jmcneill
122 1.17 jmcneill /* Fill in mandatory member */
123 1.17 jmcneill if (!prop_dictionary_set_cstring_nocopy(ev, "event", event)) {
124 1.17 jmcneill prop_object_release(ev);
125 1.17 jmcneill mutex_exit(&drvctl_lock);
126 1.17 jmcneill return;
127 1.17 jmcneill }
128 1.17 jmcneill
129 1.17 jmcneill dce = kmem_alloc(sizeof(*dce), KM_SLEEP);
130 1.17 jmcneill if (dce == NULL) {
131 1.17 jmcneill mutex_exit(&drvctl_lock);
132 1.17 jmcneill return;
133 1.17 jmcneill }
134 1.17 jmcneill
135 1.17 jmcneill dce->dce_event = ev;
136 1.17 jmcneill
137 1.17 jmcneill if (drvctl_eventcnt == DRVCTL_EVENTQ_DEPTH) {
138 1.17 jmcneill odce = TAILQ_FIRST(&drvctl_eventq);
139 1.17 jmcneill TAILQ_REMOVE(&drvctl_eventq, odce, dce_link);
140 1.18 freza prop_object_release(odce->dce_event);
141 1.18 freza kmem_free(odce, sizeof(*odce));
142 1.17 jmcneill --drvctl_eventcnt;
143 1.17 jmcneill }
144 1.17 jmcneill
145 1.17 jmcneill TAILQ_INSERT_TAIL(&drvctl_eventq, dce, dce_link);
146 1.17 jmcneill ++drvctl_eventcnt;
147 1.17 jmcneill cv_broadcast(&drvctl_cond);
148 1.20 jmcneill selnotify(&drvctl_rdsel, 0, 0);
149 1.17 jmcneill
150 1.17 jmcneill mutex_exit(&drvctl_lock);
151 1.17 jmcneill }
152 1.17 jmcneill
153 1.17 jmcneill int
154 1.17 jmcneill drvctlopen(dev_t dev, int flags, int mode, struct lwp *l)
155 1.17 jmcneill {
156 1.17 jmcneill struct file *fp;
157 1.17 jmcneill int fd;
158 1.17 jmcneill int ret;
159 1.17 jmcneill
160 1.17 jmcneill ret = fd_allocfile(&fp, &fd);
161 1.17 jmcneill if (ret)
162 1.28 dyoung return ret;
163 1.17 jmcneill
164 1.17 jmcneill /* XXX setup context */
165 1.17 jmcneill mutex_enter(&drvctl_lock);
166 1.17 jmcneill ret = fd_clone(fp, fd, flags, &drvctl_fileops, /* context */NULL);
167 1.17 jmcneill ++drvctl_nopen;
168 1.17 jmcneill mutex_exit(&drvctl_lock);
169 1.17 jmcneill
170 1.17 jmcneill return ret;
171 1.17 jmcneill }
172 1.5 thorpej
173 1.12 dyoung static int
174 1.19 gmcgarry pmdevbyname(u_long cmd, struct devpmargs *a)
175 1.12 dyoung {
176 1.12 dyoung struct device *d;
177 1.12 dyoung
178 1.14 joerg if ((d = device_find_by_xname(a->devname)) == NULL)
179 1.12 dyoung return ENXIO;
180 1.12 dyoung
181 1.12 dyoung switch (cmd) {
182 1.12 dyoung case DRVSUSPENDDEV:
183 1.29 dyoung return pmf_device_recursive_suspend(d, PMF_Q_DRVCTL) ? 0 : EBUSY;
184 1.12 dyoung case DRVRESUMEDEV:
185 1.16 dyoung if (a->flags & DEVPM_F_SUBTREE) {
186 1.29 dyoung return pmf_device_subtree_resume(d, PMF_Q_DRVCTL)
187 1.16 dyoung ? 0 : EBUSY;
188 1.16 dyoung } else {
189 1.29 dyoung return pmf_device_recursive_resume(d, PMF_Q_DRVCTL)
190 1.16 dyoung ? 0 : EBUSY;
191 1.16 dyoung }
192 1.12 dyoung default:
193 1.12 dyoung return EPASSTHROUGH;
194 1.12 dyoung }
195 1.12 dyoung }
196 1.12 dyoung
197 1.12 dyoung static int
198 1.12 dyoung listdevbyname(struct devlistargs *l)
199 1.12 dyoung {
200 1.13 drochner device_t d, child;
201 1.15 dyoung deviter_t di;
202 1.15 dyoung int cnt = 0, idx, error = 0;
203 1.12 dyoung
204 1.24 joerg if (*l->l_devname == '\0')
205 1.24 joerg d = (device_t)NULL;
206 1.24 joerg else if (memchr(l->l_devname, 0, sizeof(l->l_devname)) == NULL)
207 1.24 joerg return EINVAL;
208 1.24 joerg else if ((d = device_find_by_xname(l->l_devname)) == NULL)
209 1.12 dyoung return ENXIO;
210 1.12 dyoung
211 1.15 dyoung for (child = deviter_first(&di, 0); child != NULL;
212 1.15 dyoung child = deviter_next(&di)) {
213 1.13 drochner if (device_parent(child) != d)
214 1.13 drochner continue;
215 1.13 drochner idx = cnt++;
216 1.13 drochner if (l->l_childname == NULL || idx >= l->l_children)
217 1.13 drochner continue;
218 1.13 drochner error = copyoutstr(device_xname(child), l->l_childname[idx],
219 1.13 drochner sizeof(l->l_childname[idx]), NULL);
220 1.15 dyoung if (error != 0)
221 1.15 dyoung break;
222 1.13 drochner }
223 1.15 dyoung deviter_release(&di);
224 1.12 dyoung
225 1.13 drochner l->l_children = cnt;
226 1.15 dyoung return error;
227 1.12 dyoung }
228 1.12 dyoung
229 1.1 drochner static int
230 1.1 drochner detachdevbyname(const char *devname)
231 1.1 drochner {
232 1.1 drochner struct device *d;
233 1.1 drochner
234 1.14 joerg if ((d = device_find_by_xname(devname)) == NULL)
235 1.12 dyoung return ENXIO;
236 1.12 dyoung
237 1.1 drochner #ifndef XXXFULLRISK
238 1.12 dyoung /*
239 1.12 dyoung * If the parent cannot be notified, it might keep
240 1.12 dyoung * pointers to the detached device.
241 1.12 dyoung * There might be a private notification mechanism,
242 1.28 dyoung * but better play it safe here.
243 1.12 dyoung */
244 1.12 dyoung if (d->dv_parent && !d->dv_parent->dv_cfattach->ca_childdetached)
245 1.28 dyoung return ENOTSUP;
246 1.1 drochner #endif
247 1.28 dyoung return config_detach(d, 0);
248 1.1 drochner }
249 1.1 drochner
250 1.1 drochner static int
251 1.1 drochner rescanbus(const char *busname, const char *ifattr,
252 1.1 drochner int numlocators, const int *locators)
253 1.1 drochner {
254 1.12 dyoung int i, rc;
255 1.1 drochner struct device *d;
256 1.2 drochner const struct cfiattrdata * const *ap;
257 1.1 drochner
258 1.1 drochner /* XXX there should be a way to get limits and defaults (per device)
259 1.1 drochner from config generated data */
260 1.1 drochner int locs[MAXLOCATORS];
261 1.1 drochner for (i = 0; i < MAXLOCATORS; i++)
262 1.1 drochner locs[i] = -1;
263 1.1 drochner
264 1.1 drochner for (i = 0; i < numlocators;i++)
265 1.1 drochner locs[i] = locators[i];
266 1.1 drochner
267 1.14 joerg if ((d = device_find_by_xname(busname)) == NULL)
268 1.12 dyoung return ENXIO;
269 1.1 drochner
270 1.12 dyoung /*
271 1.12 dyoung * must support rescan, and must have something
272 1.12 dyoung * to attach to
273 1.12 dyoung */
274 1.12 dyoung if (!d->dv_cfattach->ca_rescan ||
275 1.12 dyoung !d->dv_cfdriver->cd_attrs)
276 1.28 dyoung return ENODEV;
277 1.12 dyoung
278 1.12 dyoung /* allow to omit attribute if there is exactly one */
279 1.12 dyoung if (!ifattr) {
280 1.12 dyoung if (d->dv_cfdriver->cd_attrs[1])
281 1.28 dyoung return EINVAL;
282 1.12 dyoung ifattr = d->dv_cfdriver->cd_attrs[0]->ci_name;
283 1.12 dyoung } else {
284 1.12 dyoung /* check for valid attribute passed */
285 1.12 dyoung for (ap = d->dv_cfdriver->cd_attrs; *ap; ap++)
286 1.12 dyoung if (!strcmp((*ap)->ci_name, ifattr))
287 1.12 dyoung break;
288 1.12 dyoung if (!*ap)
289 1.28 dyoung return EINVAL;
290 1.1 drochner }
291 1.1 drochner
292 1.12 dyoung rc = (*d->dv_cfattach->ca_rescan)(d, ifattr, locs);
293 1.12 dyoung config_deferred(NULL);
294 1.12 dyoung return rc;
295 1.1 drochner }
296 1.1 drochner
297 1.17 jmcneill static int
298 1.17 jmcneill drvctl_read(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
299 1.17 jmcneill int flags)
300 1.17 jmcneill {
301 1.28 dyoung return ENODEV;
302 1.17 jmcneill }
303 1.17 jmcneill
304 1.17 jmcneill static int
305 1.17 jmcneill drvctl_write(struct file *fp, off_t *offp, struct uio *uio, kauth_cred_t cred,
306 1.17 jmcneill int flags)
307 1.17 jmcneill {
308 1.28 dyoung return ENODEV;
309 1.17 jmcneill }
310 1.17 jmcneill
311 1.17 jmcneill static int
312 1.17 jmcneill drvctl_ioctl(struct file *fp, u_long cmd, void *data)
313 1.1 drochner {
314 1.1 drochner int res;
315 1.1 drochner char *ifattr;
316 1.1 drochner int *locs;
317 1.22 yamt size_t locs_sz = 0; /* XXXgcc */
318 1.1 drochner
319 1.1 drochner switch (cmd) {
320 1.12 dyoung case DRVSUSPENDDEV:
321 1.12 dyoung case DRVRESUMEDEV:
322 1.12 dyoung #define d ((struct devpmargs *)data)
323 1.12 dyoung res = pmdevbyname(cmd, d);
324 1.12 dyoung #undef d
325 1.12 dyoung break;
326 1.12 dyoung case DRVLISTDEV:
327 1.12 dyoung res = listdevbyname((struct devlistargs *)data);
328 1.12 dyoung break;
329 1.1 drochner case DRVDETACHDEV:
330 1.1 drochner #define d ((struct devdetachargs *)data)
331 1.1 drochner res = detachdevbyname(d->devname);
332 1.1 drochner #undef d
333 1.1 drochner break;
334 1.1 drochner case DRVRESCANBUS:
335 1.1 drochner #define d ((struct devrescanargs *)data)
336 1.1 drochner d->busname[sizeof(d->busname) - 1] = '\0';
337 1.1 drochner
338 1.1 drochner /* XXX better copyin? */
339 1.1 drochner if (d->ifattr[0]) {
340 1.1 drochner d->ifattr[sizeof(d->ifattr) - 1] = '\0';
341 1.1 drochner ifattr = d->ifattr;
342 1.1 drochner } else
343 1.1 drochner ifattr = 0;
344 1.1 drochner
345 1.1 drochner if (d->numlocators) {
346 1.1 drochner if (d->numlocators > MAXLOCATORS)
347 1.28 dyoung return EINVAL;
348 1.22 yamt locs_sz = d->numlocators * sizeof(int);
349 1.22 yamt locs = kmem_alloc(locs_sz, KM_SLEEP);
350 1.22 yamt res = copyin(d->locators, locs, locs_sz);
351 1.11 rmind if (res) {
352 1.22 yamt kmem_free(locs, locs_sz);
353 1.28 dyoung return res;
354 1.11 rmind }
355 1.1 drochner } else
356 1.22 yamt locs = NULL;
357 1.1 drochner res = rescanbus(d->busname, ifattr, d->numlocators, locs);
358 1.1 drochner if (locs)
359 1.22 yamt kmem_free(locs, locs_sz);
360 1.1 drochner #undef d
361 1.5 thorpej break;
362 1.5 thorpej case DRVCTLCOMMAND:
363 1.17 jmcneill res = drvctl_command(curlwp, (struct plistref *)data, cmd,
364 1.17 jmcneill fp->f_flag);
365 1.5 thorpej break;
366 1.17 jmcneill case DRVGETEVENT:
367 1.17 jmcneill res = drvctl_getevent(curlwp, (struct plistref *)data, cmd,
368 1.17 jmcneill fp->f_flag);
369 1.17 jmcneill break;
370 1.5 thorpej default:
371 1.28 dyoung return EPASSTHROUGH;
372 1.1 drochner }
373 1.28 dyoung return res;
374 1.1 drochner }
375 1.1 drochner
376 1.17 jmcneill static int
377 1.25 christos drvctl_stat(struct file *fp, struct stat *st)
378 1.25 christos {
379 1.25 christos (void)memset(st, 0, sizeof(*st));
380 1.26 christos st->st_uid = kauth_cred_geteuid(fp->f_cred);
381 1.26 christos st->st_gid = kauth_cred_getegid(fp->f_cred);
382 1.25 christos return 0;
383 1.25 christos }
384 1.25 christos
385 1.25 christos static int
386 1.20 jmcneill drvctl_poll(struct file *fp, int events)
387 1.20 jmcneill {
388 1.20 jmcneill int revents = 0;
389 1.20 jmcneill
390 1.20 jmcneill if (!TAILQ_EMPTY(&drvctl_eventq))
391 1.20 jmcneill revents |= events & (POLLIN | POLLRDNORM);
392 1.20 jmcneill else
393 1.20 jmcneill selrecord(curlwp, &drvctl_rdsel);
394 1.20 jmcneill
395 1.20 jmcneill return revents;
396 1.20 jmcneill }
397 1.20 jmcneill
398 1.20 jmcneill static int
399 1.17 jmcneill drvctl_close(struct file *fp)
400 1.17 jmcneill {
401 1.17 jmcneill struct drvctl_event *dce;
402 1.17 jmcneill
403 1.17 jmcneill /* XXX free context */
404 1.17 jmcneill mutex_enter(&drvctl_lock);
405 1.17 jmcneill KASSERT(drvctl_nopen > 0);
406 1.17 jmcneill --drvctl_nopen;
407 1.17 jmcneill if (drvctl_nopen == 0) {
408 1.17 jmcneill /* flush queue */
409 1.17 jmcneill while ((dce = TAILQ_FIRST(&drvctl_eventq)) != NULL) {
410 1.17 jmcneill TAILQ_REMOVE(&drvctl_eventq, dce, dce_link);
411 1.17 jmcneill KASSERT(drvctl_eventcnt > 0);
412 1.17 jmcneill --drvctl_eventcnt;
413 1.18 freza prop_object_release(dce->dce_event);
414 1.17 jmcneill kmem_free(dce, sizeof(*dce));
415 1.17 jmcneill }
416 1.17 jmcneill }
417 1.17 jmcneill mutex_exit(&drvctl_lock);
418 1.17 jmcneill
419 1.28 dyoung return 0;
420 1.17 jmcneill }
421 1.17 jmcneill
422 1.1 drochner void
423 1.9 yamt drvctlattach(int arg)
424 1.1 drochner {
425 1.1 drochner }
426 1.5 thorpej
427 1.5 thorpej /*****************************************************************************
428 1.5 thorpej * Driver control command processing engine
429 1.5 thorpej *****************************************************************************/
430 1.5 thorpej
431 1.5 thorpej static int
432 1.9 yamt drvctl_command_get_properties(struct lwp *l,
433 1.5 thorpej prop_dictionary_t command_dict,
434 1.5 thorpej prop_dictionary_t results_dict)
435 1.5 thorpej {
436 1.5 thorpej prop_dictionary_t args_dict;
437 1.5 thorpej prop_string_t devname_string;
438 1.5 thorpej device_t dev;
439 1.15 dyoung deviter_t di;
440 1.5 thorpej
441 1.5 thorpej args_dict = prop_dictionary_get(command_dict, "drvctl-arguments");
442 1.5 thorpej if (args_dict == NULL)
443 1.28 dyoung return EINVAL;
444 1.5 thorpej
445 1.5 thorpej devname_string = prop_dictionary_get(args_dict, "device-name");
446 1.5 thorpej if (devname_string == NULL)
447 1.28 dyoung return EINVAL;
448 1.5 thorpej
449 1.15 dyoung for (dev = deviter_first(&di, 0); dev != NULL;
450 1.15 dyoung dev = deviter_next(&di)) {
451 1.5 thorpej if (prop_string_equals_cstring(devname_string,
452 1.15 dyoung device_xname(dev))) {
453 1.15 dyoung prop_dictionary_set(results_dict, "drvctl-result-data",
454 1.15 dyoung device_properties(dev));
455 1.5 thorpej break;
456 1.15 dyoung }
457 1.5 thorpej }
458 1.5 thorpej
459 1.15 dyoung deviter_release(&di);
460 1.15 dyoung
461 1.5 thorpej if (dev == NULL)
462 1.28 dyoung return ESRCH;
463 1.15 dyoung
464 1.28 dyoung return 0;
465 1.5 thorpej }
466 1.5 thorpej
467 1.5 thorpej struct drvctl_command_desc {
468 1.5 thorpej const char *dcd_name; /* command name */
469 1.5 thorpej int (*dcd_func)(struct lwp *, /* handler function */
470 1.5 thorpej prop_dictionary_t,
471 1.5 thorpej prop_dictionary_t);
472 1.5 thorpej int dcd_rw; /* read or write required */
473 1.5 thorpej };
474 1.5 thorpej
475 1.5 thorpej static const struct drvctl_command_desc drvctl_command_table[] = {
476 1.5 thorpej { .dcd_name = "get-properties",
477 1.5 thorpej .dcd_func = drvctl_command_get_properties,
478 1.5 thorpej .dcd_rw = FREAD,
479 1.5 thorpej },
480 1.5 thorpej
481 1.5 thorpej { .dcd_name = NULL }
482 1.5 thorpej };
483 1.5 thorpej
484 1.5 thorpej static int
485 1.5 thorpej drvctl_command(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
486 1.5 thorpej int fflag)
487 1.5 thorpej {
488 1.5 thorpej prop_dictionary_t command_dict, results_dict;
489 1.5 thorpej prop_string_t command_string;
490 1.5 thorpej const struct drvctl_command_desc *dcd;
491 1.5 thorpej int error;
492 1.5 thorpej
493 1.5 thorpej error = prop_dictionary_copyin_ioctl(pref, ioctl_cmd, &command_dict);
494 1.5 thorpej if (error)
495 1.28 dyoung return error;
496 1.5 thorpej
497 1.5 thorpej results_dict = prop_dictionary_create();
498 1.5 thorpej if (results_dict == NULL) {
499 1.5 thorpej prop_object_release(command_dict);
500 1.28 dyoung return ENOMEM;
501 1.5 thorpej }
502 1.5 thorpej
503 1.5 thorpej command_string = prop_dictionary_get(command_dict, "drvctl-command");
504 1.5 thorpej if (command_string == NULL) {
505 1.5 thorpej error = EINVAL;
506 1.5 thorpej goto out;
507 1.5 thorpej }
508 1.5 thorpej
509 1.5 thorpej for (dcd = drvctl_command_table; dcd->dcd_name != NULL; dcd++) {
510 1.5 thorpej if (prop_string_equals_cstring(command_string,
511 1.5 thorpej dcd->dcd_name))
512 1.5 thorpej break;
513 1.5 thorpej }
514 1.5 thorpej
515 1.5 thorpej if (dcd->dcd_name == NULL) {
516 1.5 thorpej error = EINVAL;
517 1.5 thorpej goto out;
518 1.5 thorpej }
519 1.5 thorpej
520 1.5 thorpej if ((fflag & dcd->dcd_rw) == 0) {
521 1.5 thorpej error = EPERM;
522 1.5 thorpej goto out;
523 1.5 thorpej }
524 1.5 thorpej
525 1.5 thorpej error = (*dcd->dcd_func)(l, command_dict, results_dict);
526 1.5 thorpej
527 1.8 thorpej prop_dictionary_set_int32(results_dict, "drvctl-error", error);
528 1.5 thorpej
529 1.5 thorpej error = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, results_dict);
530 1.5 thorpej out:
531 1.5 thorpej prop_object_release(command_dict);
532 1.5 thorpej prop_object_release(results_dict);
533 1.28 dyoung return error;
534 1.5 thorpej }
535 1.17 jmcneill
536 1.17 jmcneill static int
537 1.17 jmcneill drvctl_getevent(struct lwp *l, struct plistref *pref, u_long ioctl_cmd,
538 1.17 jmcneill int fflag)
539 1.17 jmcneill {
540 1.17 jmcneill struct drvctl_event *dce;
541 1.17 jmcneill int ret;
542 1.17 jmcneill
543 1.17 jmcneill if ((fflag & (FREAD|FWRITE)) != (FREAD|FWRITE))
544 1.28 dyoung return EPERM;
545 1.17 jmcneill
546 1.17 jmcneill mutex_enter(&drvctl_lock);
547 1.17 jmcneill while ((dce = TAILQ_FIRST(&drvctl_eventq)) == NULL) {
548 1.17 jmcneill if (fflag & O_NONBLOCK) {
549 1.17 jmcneill mutex_exit(&drvctl_lock);
550 1.28 dyoung return EWOULDBLOCK;
551 1.17 jmcneill }
552 1.17 jmcneill
553 1.17 jmcneill ret = cv_wait_sig(&drvctl_cond, &drvctl_lock);
554 1.17 jmcneill if (ret) {
555 1.17 jmcneill mutex_exit(&drvctl_lock);
556 1.28 dyoung return ret;
557 1.17 jmcneill }
558 1.17 jmcneill }
559 1.17 jmcneill TAILQ_REMOVE(&drvctl_eventq, dce, dce_link);
560 1.17 jmcneill KASSERT(drvctl_eventcnt > 0);
561 1.17 jmcneill --drvctl_eventcnt;
562 1.17 jmcneill mutex_exit(&drvctl_lock);
563 1.17 jmcneill
564 1.17 jmcneill ret = prop_dictionary_copyout_ioctl(pref, ioctl_cmd, dce->dce_event);
565 1.17 jmcneill
566 1.17 jmcneill prop_object_release(dce->dce_event);
567 1.17 jmcneill kmem_free(dce, sizeof(*dce));
568 1.17 jmcneill
569 1.28 dyoung return ret;
570 1.17 jmcneill }
571