2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com> All rights reserved.
3 * cdevsw from kern/kern_conf.c Copyright (c) 1995 Terrence R. Lambert
4 * cdevsw from kern/kern_conf.c Copyright (c) 1995 Julian R. Elishcer,
6 * Copyright (c) 1982, 1986, 1991, 1993
7 * The Regents of the University of California. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * $DragonFly: src/sys/kern/kern_device.c,v 1.22 2006/12/23 00:35:04 swildner Exp $
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/sysctl.h>
36 #include <sys/systm.h>
37 #include <sys/module.h>
38 #include <sys/malloc.h>
42 #include <sys/vnode.h>
43 #include <sys/queue.h>
44 #include <sys/device.h>
45 #include <sys/syslink.h>
47 #include <machine/stdarg.h>
48 #include <sys/thread2.h>
51 * system link descriptors identify the command in the
52 * arguments structure.
54 #define DDESCNAME(name) __CONCAT(__CONCAT(dev_,name),_desc)
56 #define DEVOP_DESC_INIT(name) \
57 struct syslink_desc DDESCNAME(name) = { \
58 __offsetof(struct dev_ops, __CONCAT(d_, name)), \
61 DEVOP_DESC_INIT(default);
62 DEVOP_DESC_INIT(open);
63 DEVOP_DESC_INIT(close);
64 DEVOP_DESC_INIT(read);
65 DEVOP_DESC_INIT(write);
66 DEVOP_DESC_INIT(ioctl);
67 DEVOP_DESC_INIT(dump);
68 DEVOP_DESC_INIT(psize);
69 DEVOP_DESC_INIT(poll);
70 DEVOP_DESC_INIT(mmap);
71 DEVOP_DESC_INIT(strategy);
72 DEVOP_DESC_INIT(kqfilter);
73 DEVOP_DESC_INIT(clone);
78 struct dev_ops dead_dev_ops;
80 struct dev_ops default_dev_ops = {
82 .d_default = NULL, /* must be NULL */
90 .d_strategy = nostrategy,
93 .d_kqfilter = nokqfilter,
98 * This is used to look-up devices
100 static struct dev_ops_link *dev_ops_array[NUMCDEVSW];
102 /************************************************************************
103 * GENERAL DEVICE API FUNCTIONS *
104 ************************************************************************/
107 dev_dopen(cdev_t dev, int oflags, int devtype, struct ucred *cred)
109 struct dev_open_args ap;
111 ap.a_head.a_desc = &dev_open_desc;
112 ap.a_head.a_dev = dev;
113 ap.a_oflags = oflags;
114 ap.a_devtype = devtype;
116 return(dev->si_ops->d_open(&ap));
120 dev_dclose(cdev_t dev, int fflag, int devtype)
122 struct dev_close_args ap;
124 ap.a_head.a_desc = &dev_close_desc;
125 ap.a_head.a_dev = dev;
127 ap.a_devtype = devtype;
128 return(dev->si_ops->d_close(&ap));
132 dev_dread(cdev_t dev, struct uio *uio, int ioflag)
134 struct dev_read_args ap;
137 ap.a_head.a_desc = &dev_read_desc;
138 ap.a_head.a_dev = dev;
140 ap.a_ioflag = ioflag;
141 error = dev->si_ops->d_read(&ap);
143 dev->si_lastread = time_second;
148 dev_dwrite(cdev_t dev, struct uio *uio, int ioflag)
150 struct dev_write_args ap;
153 dev->si_lastwrite = time_second;
154 ap.a_head.a_desc = &dev_write_desc;
155 ap.a_head.a_dev = dev;
157 ap.a_ioflag = ioflag;
158 error = dev->si_ops->d_write(&ap);
163 dev_dioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag, struct ucred *cred)
165 struct dev_ioctl_args ap;
167 ap.a_head.a_desc = &dev_ioctl_desc;
168 ap.a_head.a_dev = dev;
173 return(dev->si_ops->d_ioctl(&ap));
177 dev_dpoll(cdev_t dev, int events)
179 struct dev_poll_args ap;
182 ap.a_head.a_desc = &dev_poll_desc;
183 ap.a_head.a_dev = dev;
184 ap.a_events = events;
185 error = dev->si_ops->d_poll(&ap);
188 return (seltrue(dev, events));
192 dev_dmmap(cdev_t dev, vm_offset_t offset, int nprot)
194 struct dev_mmap_args ap;
197 ap.a_head.a_desc = &dev_mmap_desc;
198 ap.a_head.a_dev = dev;
199 ap.a_offset = offset;
201 error = dev->si_ops->d_mmap(&ap);
208 dev_dclone(cdev_t dev)
210 struct dev_clone_args ap;
212 ap.a_head.a_desc = &dev_clone_desc;
213 ap.a_head.a_dev = dev;
214 return (dev->si_ops->d_clone(&ap));
218 * Core device strategy call, used to issue I/O on a device. There are
219 * two versions, a non-chained version and a chained version. The chained
220 * version reuses a BIO set up by vn_strategy(). The only difference is
221 * that, for now, we do not push a new tracking structure when chaining
222 * from vn_strategy. XXX this will ultimately have to change.
225 dev_dstrategy(cdev_t dev, struct bio *bio)
227 struct dev_strategy_args ap;
228 struct bio_track *track;
230 ap.a_head.a_desc = &dev_strategy_desc;
231 ap.a_head.a_dev = dev;
234 KKASSERT(bio->bio_track == NULL);
235 KKASSERT(bio->bio_buf->b_cmd != BUF_CMD_DONE);
236 if (bio->bio_buf->b_cmd == BUF_CMD_READ)
237 track = &dev->si_track_read;
239 track = &dev->si_track_write;
240 atomic_add_int(&track->bk_active, 1);
241 bio->bio_track = track;
242 (void)dev->si_ops->d_strategy(&ap);
246 dev_dstrategy_chain(cdev_t dev, struct bio *bio)
248 struct dev_strategy_args ap;
250 KKASSERT(bio->bio_track != NULL);
251 ap.a_head.a_desc = &dev_strategy_desc;
252 ap.a_head.a_dev = dev;
254 (void)dev->si_ops->d_strategy(&ap);
258 * note: the disk layer is expected to set count, blkno, and secsize before
259 * forwarding the message.
262 dev_ddump(cdev_t dev)
264 struct dev_dump_args ap;
266 ap.a_head.a_desc = &dev_dump_desc;
267 ap.a_head.a_dev = dev;
271 return(dev->si_ops->d_dump(&ap));
275 dev_dpsize(cdev_t dev)
277 struct dev_psize_args ap;
280 ap.a_head.a_desc = &dev_psize_desc;
281 ap.a_head.a_dev = dev;
282 error = dev->si_ops->d_psize(&ap);
284 return (ap.a_result);
289 dev_dkqfilter(cdev_t dev, struct knote *kn)
291 struct dev_kqfilter_args ap;
294 ap.a_head.a_desc = &dev_kqfilter_desc;
295 ap.a_head.a_dev = dev;
297 error = dev->si_ops->d_kqfilter(&ap);
303 /************************************************************************
304 * DEVICE HELPER FUNCTIONS *
305 ************************************************************************/
308 dev_dname(cdev_t dev)
310 return(dev->si_ops->head.name);
314 dev_dflags(cdev_t dev)
316 return(dev->si_ops->head.flags);
322 return(dev->si_ops->head.maj);
326 * Used when forwarding a request through layers. The caller adjusts
327 * ap->a_head.a_dev and then calls this function.
330 dev_doperate(struct dev_generic_args *ap)
332 int (*func)(struct dev_generic_args *);
334 func = *(void **)((char *)ap->a_dev->si_ops + ap->a_desc->sd_offset);
339 * Used by the console intercept code only. Issue an operation through
340 * a foreign ops structure allowing the ops structure associated
341 * with the device to remain intact.
344 dev_doperate_ops(struct dev_ops *ops, struct dev_generic_args *ap)
346 int (*func)(struct dev_generic_args *);
348 func = *(void **)((char *)ops + ap->a_desc->sd_offset);
353 * Convert a template dev_ops into the real thing by filling in
354 * uninitialized fields.
357 compile_dev_ops(struct dev_ops *ops)
361 for (offset = offsetof(struct dev_ops, dev_ops_first_field);
362 offset <= offsetof(struct dev_ops, dev_ops_last_field);
363 offset += sizeof(void *)
365 void **func_p = (void **)((char *)ops + offset);
366 void **def_p = (void **)((char *)&default_dev_ops + offset);
367 if (*func_p == NULL) {
369 *func_p = ops->d_default;
376 /************************************************************************
377 * MAJOR/MINOR SPACE FUNCTION *
378 ************************************************************************/
381 * This makes a dev_ops entry visible to userland (e.g /dev/<blah>).
383 * The kernel can overload a major number by making multiple dev_ops_add()
384 * calls, but only the most recent one (the first one in the dev_ops_array[]
385 * list matching the mask/match) will be visible to userland. make_dev() does
386 * not automatically call dev_ops_add() (nor do we want it to, since
387 * partition-managed disk devices are overloaded on top of the raw device).
389 * Disk devices typically register their major, e.g. 'ad0', and then call
390 * into the disk label management code which overloads its own onto e.g. 'ad0'
391 * to support all the various slice and partition combinations.
393 * The mask/match supplied in this call are a full 32 bits and the same
394 * mask and match must be specified in a later dev_ops_remove() call to
395 * match this add. However, the match value for the minor number should never
396 * have any bits set in the major number's bit range (8-15). The mask value
397 * may be conveniently specified as -1 without creating any major number
401 dev_ops_add(struct dev_ops *ops, u_int mask, u_int match)
404 struct dev_ops_link *link;
406 compile_dev_ops(ops);
408 if (maj < 0 || maj >= NUMCDEVSW) {
409 kprintf("%s: ERROR: driver has bogus dev_ops->head.maj = %d\n",
410 ops->head.name, maj);
413 for (link = dev_ops_array[maj]; link; link = link->next) {
415 * If we get an exact match we usurp the target, but we only print
416 * a warning message if a different device switch is installed.
418 if (link->mask == mask && link->match == match) {
419 if (link->ops != ops) {
420 kprintf("WARNING: \"%s\" (%p) is usurping \"%s\"'s"
421 " (%p) dev_ops_array[]\n",
423 link->ops->head.name, link->ops);
430 * XXX add additional warnings for overlaps
434 link = kmalloc(sizeof(struct dev_ops_link), M_DEVBUF, M_INTWAIT|M_ZERO);
438 link->next = dev_ops_array[maj];
439 dev_ops_array[maj] = link;
445 * Should only be used by udev2dev().
447 * If the minor number is -1, we match the first ops we find for this
448 * major. If the mask is not -1 then multiple minor numbers can match
451 * Note that this function will return NULL if the minor number is not within
452 * the bounds of the installed mask(s).
454 * The specified minor number should NOT include any major bits.
457 dev_ops_get(int x, int y)
459 struct dev_ops_link *link;
461 if (x < 0 || x >= NUMCDEVSW)
463 for (link = dev_ops_array[x]; link; link = link->next) {
464 if (y == -1 || (link->mask & y) == link->match)
471 * Take a cookie cutter to the major/minor device space for the passed
472 * device and generate a new dev_ops visible to userland which the caller
473 * can then modify. The original device is not modified but portions of
474 * its major/minor space will no longer be visible to userland.
477 dev_ops_add_override(cdev_t backing_dev, struct dev_ops *template,
478 u_int mask, u_int match)
481 struct dev_ops *backing_ops = backing_dev->si_ops;
483 ops = kmalloc(sizeof(struct dev_ops), M_DEVBUF, M_INTWAIT);
485 ops->head.name = backing_ops->head.name;
486 ops->head.maj = backing_ops->head.maj;
487 ops->head.flags = backing_ops->head.flags;
488 compile_dev_ops(ops);
489 dev_ops_add(ops, mask, match);
495 * Remove all matching dev_ops entries from the dev_ops_array[] major
496 * array so no new user opens can be performed, and destroy all devices
497 * installed in the hash table that are associated with this dev_ops. (see
498 * destroy_all_devs()).
500 * The mask and match should match a previous call to dev_ops_add*().
503 dev_ops_remove(struct dev_ops *ops, u_int mask, u_int match)
505 int maj = ops->head.maj;
506 struct dev_ops_link *link;
507 struct dev_ops_link **plink;
509 if (maj < 0 || maj >= NUMCDEVSW) {
510 kprintf("%s: ERROR: driver has bogus ops->d_maj = %d\n",
511 ops->head.name, maj);
514 if (ops != &dead_dev_ops)
515 destroy_all_devs(ops, mask, match);
516 for (plink = &dev_ops_array[maj]; (link = *plink) != NULL;
517 plink = &link->next) {
518 if (link->mask == mask && link->match == match) {
519 if (link->ops == ops)
521 kprintf("%s: ERROR: cannot remove from dev_ops_array[], "
522 "its major number %d was stolen by %s\n",
529 kprintf("%s(%d)[%08x/%08x]: WARNING: ops removed "
531 ops->head.name, maj, mask, match);
534 --ops->head.refs; /* XXX ops_release() / record refs */
535 kfree(link, M_DEVBUF);
537 if (dev_ops_array[maj] == NULL && ops->head.refs != 0) {
538 kprintf("%s(%d)[%08x/%08x]: Warning: dev_ops_remove() called "
539 "while %d device refs still exist!\n",
540 ops->head.name, maj, mask, match, ops->head.refs);
542 kprintf("%s: ops removed\n", ops->head.name);
548 * Release a ops entry. When the ref count reaches zero, recurse
552 dev_ops_release(struct dev_ops *ops)
555 if (ops->head.refs == 0) {
561 dev_ops_intercept(cdev_t dev, struct dev_ops *iops)
563 struct dev_ops *oops = dev->si_ops;
565 compile_dev_ops(iops);
566 iops->head.maj = oops->head.maj;
567 iops->head.data = oops->head.data;
568 iops->head.flags = oops->head.flags;
570 dev->si_flags |= SI_INTERCEPTED;
576 dev_ops_restore(cdev_t dev, struct dev_ops *oops)
578 struct dev_ops *iops = dev->si_ops;
581 dev->si_flags &= ~SI_INTERCEPTED;
583 iops->head.data = NULL;
584 iops->head.flags = 0;
587 /************************************************************************
588 * DEFAULT DEV OPS FUNCTIONS *
589 ************************************************************************/
593 * Unsupported devswitch functions (e.g. for writing to read-only device).
594 * XXX may belong elsewhere.
598 noclone(struct dev_clone_args *ap)
601 return (0); /* allow the clone */
605 noopen(struct dev_open_args *ap)
611 noclose(struct dev_close_args *ap)
617 noread(struct dev_read_args *ap)
623 nowrite(struct dev_write_args *ap)
629 noioctl(struct dev_ioctl_args *ap)
635 nokqfilter(struct dev_kqfilter_args *ap)
641 nommap(struct dev_mmap_args *ap)
647 nopoll(struct dev_poll_args *ap)
654 nostrategy(struct dev_strategy_args *ap)
656 struct bio *bio = ap->a_bio;
658 bio->bio_buf->b_flags |= B_ERROR;
659 bio->bio_buf->b_error = EOPNOTSUPP;
665 nopsize(struct dev_psize_args *ap)
672 nodump(struct dev_dump_args *ap)
678 * XXX this is probably bogus. Any device that uses it isn't checking the
682 nullopen(struct dev_open_args *ap)
688 nullclose(struct dev_close_args *ap)