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.27 2007/07/23 18:59:50 dillon 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>
46 #include <sys/syslink_rpc.h>
48 #include <machine/stdarg.h>
49 #include <sys/thread2.h>
50 #include <sys/devfs.h>
53 * system link descriptors identify the command in the
54 * arguments structure.
56 #define DDESCNAME(name) __CONCAT(__CONCAT(dev_,name),_desc)
58 #define DEVOP_DESC_INIT(name) \
59 struct syslink_desc DDESCNAME(name) = { \
60 __offsetof(struct dev_ops, __CONCAT(d_, name)), \
63 DEVOP_DESC_INIT(default);
64 DEVOP_DESC_INIT(open);
65 DEVOP_DESC_INIT(close);
66 DEVOP_DESC_INIT(read);
67 DEVOP_DESC_INIT(write);
68 DEVOP_DESC_INIT(ioctl);
69 DEVOP_DESC_INIT(dump);
70 DEVOP_DESC_INIT(psize);
71 DEVOP_DESC_INIT(poll);
72 DEVOP_DESC_INIT(mmap);
73 DEVOP_DESC_INIT(strategy);
74 DEVOP_DESC_INIT(kqfilter);
75 DEVOP_DESC_INIT(revoke);
76 DEVOP_DESC_INIT(clone);
81 struct dev_ops dead_dev_ops;
83 struct dev_ops default_dev_ops = {
85 .d_default = NULL, /* must be NULL */
93 .d_strategy = nostrategy,
96 .d_kqfilter = nokqfilter,
101 /************************************************************************
102 * GENERAL DEVICE API FUNCTIONS *
103 ************************************************************************/
106 dev_dopen(cdev_t dev, int oflags, int devtype, struct ucred *cred)
108 struct dev_open_args ap;
110 ap.a_head.a_desc = &dev_open_desc;
111 ap.a_head.a_dev = dev;
112 ap.a_oflags = oflags;
113 ap.a_devtype = devtype;
115 return(dev->si_ops->d_open(&ap));
119 dev_dclose(cdev_t dev, int fflag, int devtype)
121 struct dev_close_args ap;
123 ap.a_head.a_desc = &dev_close_desc;
124 ap.a_head.a_dev = dev;
126 ap.a_devtype = devtype;
127 return(dev->si_ops->d_close(&ap));
131 dev_dread(cdev_t dev, struct uio *uio, int ioflag)
133 struct dev_read_args ap;
136 ap.a_head.a_desc = &dev_read_desc;
137 ap.a_head.a_dev = dev;
139 ap.a_ioflag = ioflag;
140 error = dev->si_ops->d_read(&ap);
142 dev->si_lastread = time_second;
147 dev_dwrite(cdev_t dev, struct uio *uio, int ioflag)
149 struct dev_write_args ap;
152 dev->si_lastwrite = time_second;
153 ap.a_head.a_desc = &dev_write_desc;
154 ap.a_head.a_dev = dev;
156 ap.a_ioflag = ioflag;
157 error = dev->si_ops->d_write(&ap);
162 dev_dioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag, struct ucred *cred)
164 struct dev_ioctl_args ap;
166 ap.a_head.a_desc = &dev_ioctl_desc;
167 ap.a_head.a_dev = dev;
172 return(dev->si_ops->d_ioctl(&ap));
176 dev_dpoll(cdev_t dev, int events)
178 struct dev_poll_args ap;
181 ap.a_head.a_desc = &dev_poll_desc;
182 ap.a_head.a_dev = dev;
183 ap.a_events = events;
184 error = dev->si_ops->d_poll(&ap);
187 return (seltrue(dev, events));
191 dev_dmmap(cdev_t dev, vm_offset_t offset, int nprot)
193 struct dev_mmap_args ap;
196 ap.a_head.a_desc = &dev_mmap_desc;
197 ap.a_head.a_dev = dev;
198 ap.a_offset = offset;
200 error = dev->si_ops->d_mmap(&ap);
207 dev_dclone(cdev_t dev)
209 struct dev_clone_args ap;
211 ap.a_head.a_desc = &dev_clone_desc;
212 ap.a_head.a_dev = dev;
213 return (dev->si_ops->d_clone(&ap));
217 dev_drevoke(cdev_t dev)
219 struct dev_revoke_args ap;
221 ap.a_head.a_desc = &dev_revoke_desc;
222 ap.a_head.a_dev = dev;
223 return (dev->si_ops->d_revoke(&ap));
227 * Core device strategy call, used to issue I/O on a device. There are
228 * two versions, a non-chained version and a chained version. The chained
229 * version reuses a BIO set up by vn_strategy(). The only difference is
230 * that, for now, we do not push a new tracking structure when chaining
231 * from vn_strategy. XXX this will ultimately have to change.
234 dev_dstrategy(cdev_t dev, struct bio *bio)
236 struct dev_strategy_args ap;
237 struct bio_track *track;
239 ap.a_head.a_desc = &dev_strategy_desc;
240 ap.a_head.a_dev = dev;
243 KKASSERT(bio->bio_track == NULL);
244 KKASSERT(bio->bio_buf->b_cmd != BUF_CMD_DONE);
245 if (bio->bio_buf->b_cmd == BUF_CMD_READ)
246 track = &dev->si_track_read;
248 track = &dev->si_track_write;
249 bio_track_ref(track);
250 bio->bio_track = track;
251 KKASSERT((bio->bio_flags & BIO_DONE) == 0);
252 (void)dev->si_ops->d_strategy(&ap);
256 dev_dstrategy_chain(cdev_t dev, struct bio *bio)
258 struct dev_strategy_args ap;
260 ap.a_head.a_desc = &dev_strategy_desc;
261 ap.a_head.a_dev = dev;
264 KKASSERT(bio->bio_track != NULL);
265 KKASSERT((bio->bio_flags & BIO_DONE) == 0);
266 (void)dev->si_ops->d_strategy(&ap);
270 * note: the disk layer is expected to set count, blkno, and secsize before
271 * forwarding the message.
274 dev_ddump(cdev_t dev)
276 struct dev_dump_args ap;
278 ap.a_head.a_desc = &dev_dump_desc;
279 ap.a_head.a_dev = dev;
283 return(dev->si_ops->d_dump(&ap));
287 dev_dpsize(cdev_t dev)
289 struct dev_psize_args ap;
292 ap.a_head.a_desc = &dev_psize_desc;
293 ap.a_head.a_dev = dev;
294 error = dev->si_ops->d_psize(&ap);
296 return (ap.a_result);
301 dev_dkqfilter(cdev_t dev, struct knote *kn)
303 struct dev_kqfilter_args ap;
306 ap.a_head.a_desc = &dev_kqfilter_desc;
307 ap.a_head.a_dev = dev;
309 error = dev->si_ops->d_kqfilter(&ap);
315 /************************************************************************
316 * DEVICE HELPER FUNCTIONS *
317 ************************************************************************/
323 dev_drefs(cdev_t dev)
325 return(dev->si_sysref.refcnt);
332 dev_dname(cdev_t dev)
334 return(dev->si_ops->head.name);
341 dev_dflags(cdev_t dev)
343 return(dev->si_ops->head.flags);
352 return(dev->si_ops->head.maj);
356 * Used when forwarding a request through layers. The caller adjusts
357 * ap->a_head.a_dev and then calls this function.
360 dev_doperate(struct dev_generic_args *ap)
362 int (*func)(struct dev_generic_args *);
364 func = *(void **)((char *)ap->a_dev->si_ops + ap->a_desc->sd_offset);
369 * Used by the console intercept code only. Issue an operation through
370 * a foreign ops structure allowing the ops structure associated
371 * with the device to remain intact.
374 dev_doperate_ops(struct dev_ops *ops, struct dev_generic_args *ap)
376 int (*func)(struct dev_generic_args *);
378 func = *(void **)((char *)ops + ap->a_desc->sd_offset);
383 * Convert a template dev_ops into the real thing by filling in
384 * uninitialized fields.
387 compile_dev_ops(struct dev_ops *ops)
391 for (offset = offsetof(struct dev_ops, dev_ops_first_field);
392 offset <= offsetof(struct dev_ops, dev_ops_last_field);
393 offset += sizeof(void *)
395 void **func_p = (void **)((char *)ops + offset);
396 void **def_p = (void **)((char *)&default_dev_ops + offset);
397 if (*func_p == NULL) {
399 *func_p = ops->d_default;
406 /************************************************************************
407 * MAJOR/MINOR SPACE FUNCTION *
408 ************************************************************************/
411 * This makes a dev_ops entry visible to userland (e.g /dev/<blah>).
413 * Disk devices typically register their major, e.g. 'ad0', and then call
414 * into the disk label management code which overloads its own onto e.g. 'ad0'
415 * to support all the various slice and partition combinations.
417 * The mask/match supplied in this call are a full 32 bits and the same
418 * mask and match must be specified in a later dev_ops_remove() call to
419 * match this add. However, the match value for the minor number should never
420 * have any bits set in the major number's bit range (8-15). The mask value
421 * may be conveniently specified as -1 without creating any major number
427 rb_dev_ops_compare(struct dev_ops_maj *a, struct dev_ops_maj *b)
431 else if (a->maj > b->maj)
436 RB_GENERATE2(dev_ops_rb_tree, dev_ops_maj, rbnode, rb_dev_ops_compare, int, maj);
438 struct dev_ops_rb_tree dev_ops_rbhead = RB_INITIALIZER(dev_ops_rbhead);
441 dev_ops_remove_all(struct dev_ops *ops)
443 return devfs_destroy_dev_by_ops(ops, -1);
447 dev_ops_remove_minor(struct dev_ops *ops, int minor)
449 return devfs_destroy_dev_by_ops(ops, minor);
453 * Release a ops entry. When the ref count reaches zero, recurse
457 dev_ops_release(struct dev_ops *ops)
461 if (ops->head.refs == 0) {
467 dev_ops_intercept(cdev_t dev, struct dev_ops *iops)
469 struct dev_ops *oops = dev->si_ops;
471 compile_dev_ops(iops);
472 iops->head.maj = oops->head.maj;
473 iops->head.data = oops->head.data;
474 iops->head.flags = oops->head.flags;
476 dev->si_flags |= SI_INTERCEPTED;
482 dev_ops_restore(cdev_t dev, struct dev_ops *oops)
484 struct dev_ops *iops = dev->si_ops;
487 dev->si_flags &= ~SI_INTERCEPTED;
489 iops->head.data = NULL;
490 iops->head.flags = 0;
493 /************************************************************************
494 * DEFAULT DEV OPS FUNCTIONS *
495 ************************************************************************/
499 * Unsupported devswitch functions (e.g. for writing to read-only device).
500 * XXX may belong elsewhere.
503 norevoke(struct dev_revoke_args *ap)
510 noclone(struct dev_clone_args *ap)
513 return (0); /* allow the clone */
517 noopen(struct dev_open_args *ap)
523 noclose(struct dev_close_args *ap)
529 noread(struct dev_read_args *ap)
535 nowrite(struct dev_write_args *ap)
541 noioctl(struct dev_ioctl_args *ap)
547 nokqfilter(struct dev_kqfilter_args *ap)
553 nommap(struct dev_mmap_args *ap)
559 nopoll(struct dev_poll_args *ap)
566 nostrategy(struct dev_strategy_args *ap)
568 struct bio *bio = ap->a_bio;
570 bio->bio_buf->b_flags |= B_ERROR;
571 bio->bio_buf->b_error = EOPNOTSUPP;
577 nopsize(struct dev_psize_args *ap)
584 nodump(struct dev_dump_args *ap)
590 * XXX this is probably bogus. Any device that uses it isn't checking the
594 nullopen(struct dev_open_args *ap)
600 nullclose(struct dev_close_args *ap)