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