2 * Copyright (c) 1999-2002 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
33 #include <sys/mutex.h>
34 #include <sys/sysctl.h>
35 #include <sys/module.h>
36 #include <sys/malloc.h>
38 #include <sys/vnode.h>
39 #include <sys/queue.h>
40 #include <sys/ctype.h>
41 #include <machine/stdarg.h>
44 static struct cdevsw *cdevsw[NUMCDEVSW];
47 static MALLOC_DEFINE(M_DEVT, "dev_t", "dev_t storage");
50 * This is the number of hash-buckets. Experiements with 'real-life'
51 * udev_t's show that a prime halfway between two powers of two works
56 /* The number of dev_t's we can create before malloc(9) kick in. */
59 static struct cdev devt_stash[DEVT_STASH];
61 static LIST_HEAD(, cdev) dev_hash[DEVT_HASH];
63 static LIST_HEAD(, cdev) dev_free;
65 devfs_create_t *devfs_create_hook;
66 devfs_destroy_t *devfs_destroy_hook;
68 static int ready_for_devs;
71 SYSCTL_INT(_debug, OID_AUTO, free_devt, CTLFLAG_RW, &free_devt, 0, "");
73 /* XXX: This is a hack */
74 void disk_dev_synth(dev_t dev);
80 return (dev->si_devsw);
81 /* XXX: Hack around our backwards disk code */
84 return (dev->si_devsw);
88 return(cdevsw[major(dev)]);
97 cdevsw_add(struct cdevsw *newentry)
100 if (newentry->d_maj < 0 || newentry->d_maj >= NUMCDEVSW) {
101 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
102 newentry->d_name, newentry->d_maj);
106 if (cdevsw[newentry->d_maj]) {
107 printf("WARNING: \"%s\" is usurping \"%s\"'s cdevsw[]\n",
108 newentry->d_name, cdevsw[newentry->d_maj]->d_name);
111 cdevsw[newentry->d_maj] = newentry;
118 * Remove a cdevsw entry
122 cdevsw_remove(struct cdevsw *oldentry)
125 if (oldentry->d_maj < 0 || oldentry->d_maj >= NUMCDEVSW) {
126 printf("%s: ERROR: driver has bogus cdevsw->d_maj = %d\n",
127 oldentry->d_name, oldentry->d_maj);
131 cdevsw[oldentry->d_maj] = NULL;
138 * dev_t and u_dev_t primitives
146 return((x->si_udev >> 8) & 0xff);
154 return(x->si_udev & 0xffff00ff);
165 return ((i & 0xff) | (i >> 8));
172 KASSERT(unit <= 0xffffff, ("Invalid unit (%d) in unit2minor", unit));
173 return ((unit & 0xff) | ((unit << 8) & ~0xffff));
182 if (LIST_FIRST(&dev_free)) {
183 si = LIST_FIRST(&dev_free);
184 LIST_REMOVE(si, si_hash);
185 } else if (stashed >= DEVT_STASH) {
186 MALLOC(si, struct cdev *, sizeof(*si), M_DEVT,
187 M_USE_RESERVE | M_ZERO);
189 si = devt_stash + stashed++;
190 bzero(si, sizeof *si);
191 si->si_flags |= SI_STASHED;
193 LIST_INIT(&si->si_children);
194 TAILQ_INIT(&si->si_snapshots);
199 makedev(int x, int y)
205 if (x == umajor(NOUDEV) && y == uminor(NOUDEV))
206 panic("makedev of NOUDEV");
208 hash = udev % DEVT_HASH;
209 LIST_FOREACH(si, &dev_hash[hash], si_hash) {
210 if (si->si_udev == udev)
215 LIST_INSERT_HEAD(&dev_hash[hash], si, si_hash);
225 if (SLIST_FIRST(&dev->si_hlist))
227 if (dev->si_devsw || dev->si_drv1 || dev->si_drv2)
229 LIST_REMOVE(dev, si_hash);
230 if (dev->si_flags & SI_STASHED) {
231 bzero(dev, sizeof(*dev));
232 dev->si_flags |= SI_STASHED;
233 LIST_INSERT_HEAD(&dev_free, dev, si_hash);
248 udev2dev(udev_t x, int b)
255 return makedev(umajor(x), uminor(x));
259 Debugger("udev2dev(...,X)");
267 return(dev & 0xffff00ff);
273 return((dev & 0xff00) >> 8);
277 makeudev(int x, int y)
279 return ((x << 8) | y);
283 make_dev(struct cdevsw *devsw, int minor, uid_t uid, gid_t gid, int perms, const char *fmt, ...)
289 KASSERT(umajor(makeudev(devsw->d_maj, minor)) == devsw->d_maj,
290 ("Invalid minor (%d) in make_dev", minor));
292 if (!ready_for_devs) {
293 printf("WARNING: Driver mistake: make_dev(%s) called before SI_SUB_DRIVERS\n",
295 /* XXX panic here once drivers are cleaned up */
298 dev = makedev(devsw->d_maj, minor);
299 if (dev->si_flags & SI_NAMED) {
300 printf( "WARNING: Driver mistake: repeat make_dev(\"%s\")\n",
302 panic("don't do that");
306 i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
307 dev->si_name[i] = '\0';
309 dev->si_devsw = devsw;
312 dev->si_mode = perms;
313 dev->si_flags |= SI_NAMED;
315 if (devfs_create_hook)
316 devfs_create_hook(dev);
321 dev_named(dev_t pdev, const char *name)
325 if (strcmp(devtoname(pdev), name) == 0)
327 LIST_FOREACH(cdev, &pdev->si_children, si_siblings)
328 if (strcmp(devtoname(cdev), name) == 0)
334 dev_depends(dev_t pdev, dev_t cdev)
337 cdev->si_parent = pdev;
338 cdev->si_flags |= SI_CHILD;
339 LIST_INSERT_HEAD(&pdev->si_children, cdev, si_siblings);
343 make_dev_alias(dev_t pdev, const char *fmt, ...)
350 dev->si_flags |= SI_ALIAS;
351 dev->si_flags |= SI_NAMED;
352 dev_depends(pdev, dev);
354 i = kvprintf(fmt, NULL, dev->si_name, 32, ap);
355 dev->si_name[i] = '\0';
358 if (devfs_create_hook)
359 devfs_create_hook(dev);
364 revoke_and_destroy_dev(dev_t dev)
370 vp = SLIST_FIRST(&dev->si_hlist);
372 VOP_REVOKE(vp, REVOKEALL);
377 destroy_dev(dev_t dev)
380 if (!(dev->si_flags & SI_NAMED)) {
381 printf( "WARNING: Driver mistake: destroy_dev on %d/%d\n",
382 major(dev), minor(dev));
383 panic("don't do that");
387 if (devfs_destroy_hook)
388 devfs_destroy_hook(dev);
389 if (dev->si_flags & SI_CHILD) {
390 LIST_REMOVE(dev, si_siblings);
391 dev->si_flags &= ~SI_CHILD;
393 while (!LIST_EMPTY(&dev->si_children))
394 destroy_dev(LIST_FIRST(&dev->si_children));
398 bzero(&dev->__si_u, sizeof(dev->__si_u));
399 dev->si_flags &= ~SI_NAMED;
400 dev->si_flags &= ~SI_ALIAS;
410 if (dev->si_name[0] == '#' || dev->si_name[0] == '\0') {
413 sprintf(p, "#%s/", devsw(dev)->d_name);
415 sprintf(p, "#%d/", major(dev));
418 if (mynor < 0 || mynor > 255)
419 sprintf(p, "%#x", (u_int)mynor);
421 sprintf(p, "%d", mynor);
423 return (dev->si_name);
427 dev_stdclone(char *name, char **namep, const char *stem, int *unit)
432 if (bcmp(stem, name, i) != 0)
434 if (!isdigit(name[i]))
437 if (name[i] == '0' && isdigit(name[i+1]))
439 while (isdigit(name[i])) {
441 u += name[i++] - '0';
454 * Helper sysctl for devname(3). We're given a {u}dev_t and return
455 * the name, if any, registered by the device driver.
458 sysctl_devname(SYSCTL_HANDLER_ARGS)
464 error = SYSCTL_IN(req, &ud, sizeof (ud));
469 dev = makedev(umajor(ud), uminor(ud));
470 if (dev->si_name[0] == '\0')
473 error = SYSCTL_OUT(req, dev->si_name, strlen(dev->si_name) + 1);
478 SYSCTL_PROC(_kern, OID_AUTO, devname, CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_ANYBODY,
479 NULL, 0, sysctl_devname, "", "devname(3) handler");
482 * Set ready_for_devs; prior to this point, device creation is not allowed.
485 dev_set_ready(void *junk)
490 SYSINIT(dev_ready, SI_SUB_DEVFS, SI_ORDER_FIRST, dev_set_ready, NULL);