2 * Copyright (c) 2003,2004,2007 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/sys/device.h,v 1.11 2007/05/17 03:02:00 dillon Exp $
37 #ifndef _SYS_DEVICE_H_
38 #define _SYS_DEVICE_H_
41 #include <sys/types.h>
46 #ifndef _SYS_SYSLINK_RPC_H_
47 #include <sys/syslink_rpc.h>
55 * This structure is at the base of every device args structure
57 struct dev_generic_args {
58 struct syslink_desc *a_desc;
62 typedef struct dev_generic_args dev_default_args;
65 * int d_open(cdev_t dev, int oflags, int devtype, struct ucred *cred)
67 struct dev_open_args {
68 struct dev_generic_args a_head;
75 * int d_close(cdev_t dev, int fflag, int devtype)
77 struct dev_close_args {
78 struct dev_generic_args a_head;
84 * int d_read(cdev_t dev, struct uio *uio, int ioflag)
86 struct dev_read_args {
87 struct dev_generic_args a_head;
93 * int d_write(cdev_t dev, struct uio *uio, int ioflag)
95 struct dev_write_args {
96 struct dev_generic_args a_head;
102 * int d_ioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag,
103 * struct ucred *cred, struct sysmsg *msg)
105 struct dev_ioctl_args {
106 struct dev_generic_args a_head;
110 struct ucred *a_cred;
111 struct sysmsg *a_sysmsg;
115 * int d_poll(cdev_t dev, int events)
117 struct dev_poll_args {
118 struct dev_generic_args a_head;
123 * int d_mmap(cdev_t dev, vm_offset_t offset, int nprot)
125 struct dev_mmap_args {
126 struct dev_generic_args a_head;
127 vm_offset_t a_offset;
129 int a_result; /* page number */
133 * void d_strategy(cdev_t dev, struct bio *bio)
135 struct dev_strategy_args {
136 struct dev_generic_args a_head;
141 * void d_dump(cdev_t dev, void *virtual, vm_offset_t physical,
142 off_t offset, size_t length)
144 struct dev_dump_args {
145 struct dev_generic_args a_head;
150 vm_offset_t a_physical;
156 * int d_psize(cdev_t dev)
158 struct dev_psize_args {
159 struct dev_generic_args a_head;
164 * int d_kqfilter(cdev_t dev, struct knote *kn)
166 struct dev_kqfilter_args {
167 struct dev_generic_args a_head;
173 * int d_clone(cdev_t dev);
175 struct dev_clone_args {
176 struct dev_generic_args a_head;
181 struct ucred *a_cred;
186 * int d_revoke(cdev_t dev)
188 struct dev_revoke_args {
189 struct dev_generic_args a_head;
193 * Typedefs to help drivers declare the driver routines and such
195 typedef int d_default_t (struct dev_generic_args *ap);
196 typedef int d_open_t (struct dev_open_args *ap);
197 typedef int d_close_t (struct dev_close_args *ap);
198 typedef int d_read_t (struct dev_read_args *ap);
199 typedef int d_write_t (struct dev_write_args *ap);
200 typedef int d_ioctl_t (struct dev_ioctl_args *ap);
201 typedef int d_poll_t (struct dev_poll_args *ap);
202 typedef int d_mmap_t (struct dev_mmap_args *ap);
203 typedef int d_strategy_t (struct dev_strategy_args *ap);
204 typedef int d_dump_t (struct dev_dump_args *ap);
205 typedef int d_psize_t (struct dev_psize_args *ap);
206 typedef int d_kqfilter_t (struct dev_kqfilter_args *ap);
207 typedef int d_clone_t (struct dev_clone_args *ap);
208 typedef int d_revoke_t (struct dev_revoke_args *ap);
211 * Character device switch table.
213 * NOTE: positions are hard coded for static structure initialization.
217 const char *name; /* base name, e.g. 'da' */
218 int maj; /* major device number */
219 u_int flags; /* D_XXX flags */
220 void *data; /* custom driver data */
221 int refs; /* ref count */
225 #define dev_ops_first_field d_default
226 d_default_t *d_default;
234 d_strategy_t *d_strategy;
237 d_kqfilter_t *d_kqfilter;
238 d_clone_t *d_clone; /* clone from base dev_ops */
239 d_revoke_t *d_revoke;
240 #define dev_ops_last_field d_revoke
246 #define D_TAPE 0x0001
247 #define D_DISK 0x0002
251 #define D_TYPEMASK 0xffff
252 #define D_SEEKABLE (D_TAPE | D_DISK | D_MEM)
257 #define D_MEMDISK 0x00010000 /* memory type disk */
258 #define D_NAGGED 0x00020000 /* nagged about missing make_dev() */
259 #define D_CANFREE 0x00040000 /* can free blocks */
260 #define D_TRACKCLOSE 0x00080000 /* track all closes */
261 #define D_MASTER 0x00100000 /* used by pty/tty code */
262 #define D_KQFILTER 0x00200000 /* has kqfilter entry */
263 #define D_MPSAFE_READ 0x00400000 /* doesn't require mplock for reads */
264 #define D_MPSAFE_WRITE 0x00800000 /* doesn't require mplock for writes */
265 #define D_MPSAFE_IOCTL 0x01000000 /* doesn't require mplock for ioctls */
268 * A union of all possible argument structures.
270 union dev_args_union {
271 struct dev_generic_args du_head;
272 struct dev_open_args du_open;
273 struct dev_close_args du_close;
274 struct dev_read_args du_read;
275 struct dev_write_args du_write;
276 struct dev_ioctl_args du_ioctl;
277 struct dev_poll_args du_poll;
278 struct dev_mmap_args du_mmap;
279 struct dev_strategy_args du_strategy;
280 struct dev_dump_args du_dump;
281 struct dev_psize_args du_psize;
282 struct dev_kqfilter_args du_kqfilter;
283 struct dev_clone_args du_clone;
287 * Linking structure for mask/match registration
289 struct dev_ops_link {
290 struct dev_ops_link *next;
297 RB_ENTRY(dev_ops_maj) rbnode; /* red-black tree of major nums */
298 struct dev_ops_link *link;
302 RB_HEAD(dev_ops_rb_tree, dev_ops_maj);
303 RB_PROTOTYPE2(dev_ops_rb_tree, dev_ops_maj, rbnode, rb_dev_ops_compare, int);
307 extern struct dev_ops dead_dev_ops;
312 int dev_dopen(cdev_t dev, int oflags, int devtype, struct ucred *cred);
313 int dev_dclose(cdev_t dev, int fflag, int devtype);
314 void dev_dstrategy(cdev_t dev, struct bio *bio);
315 void dev_dstrategy_chain(cdev_t dev, struct bio *bio);
316 int dev_dioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag,
317 struct ucred *cred, struct sysmsg *msg);
318 int dev_ddump(cdev_t dev, void *virtual, vm_offset_t physical, off_t offset,
320 int64_t dev_dpsize(cdev_t dev);
321 int dev_dread(cdev_t dev, struct uio *uio, int ioflag);
322 int dev_dwrite(cdev_t dev, struct uio *uio, int ioflag);
323 int dev_dpoll(cdev_t dev, int events);
324 int dev_dkqfilter(cdev_t dev, struct knote *kn);
325 int dev_dmmap(cdev_t dev, vm_offset_t offset, int nprot);
326 int dev_dclone(cdev_t dev);
327 int dev_drevoke(cdev_t dev);
329 int dev_drefs(cdev_t dev);
330 const char *dev_dname(cdev_t dev);
331 int dev_dmaj(cdev_t dev);
332 int dev_dflags(cdev_t dev);
333 int dev_doperate(struct dev_generic_args *ap);
334 int dev_doperate_ops(struct dev_ops *, struct dev_generic_args *ap);
336 d_default_t nodefault;
344 d_strategy_t nostrategy;
347 d_kqfilter_t nokqfilter;
354 extern struct syslink_desc dev_default_desc;
355 extern struct syslink_desc dev_open_desc;
356 extern struct syslink_desc dev_close_desc;
357 extern struct syslink_desc dev_read_desc;
358 extern struct syslink_desc dev_write_desc;
359 extern struct syslink_desc dev_ioctl_desc;
360 extern struct syslink_desc dev_dump_desc;
361 extern struct syslink_desc dev_psize_desc;
362 extern struct syslink_desc dev_poll_desc;
363 extern struct syslink_desc dev_mmap_desc;
364 extern struct syslink_desc dev_strategu_desc;
365 extern struct syslink_desc dev_kqfilter_desc;
366 extern struct syslink_desc dev_clone_desc;
368 void compile_dev_ops(struct dev_ops *);
369 int dev_ops_remove_all(struct dev_ops *ops);
370 int dev_ops_remove_minor(struct dev_ops *ops, int minor);
371 struct dev_ops *dev_ops_intercept(cdev_t, struct dev_ops *);
372 void dev_ops_restore(cdev_t, struct dev_ops *);
374 cdev_t make_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
375 int perms, const char *fmt, ...) __printflike(6, 7);
376 cdev_t make_dev_covering(struct dev_ops *ops, struct dev_ops *bops, int minor,
377 uid_t uid, gid_t gid, int perms, const char *fmt, ...) __printflike(7, 8);
378 cdev_t make_only_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
379 int perms, const char *fmt, ...) __printflike(6, 7);
380 cdev_t make_only_devfs_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
381 int perms, const char *fmt, ...) __printflike(6, 7);
382 void destroy_only_dev(cdev_t dev);
383 int make_dev_alias(cdev_t target, const char *fmt, ...) __printflike(2, 3);
384 cdev_t make_autoclone_dev(struct dev_ops *ops, struct devfs_bitmap *bitmap,
385 d_clone_t *nhandler, uid_t uid, gid_t gid, int perms,
386 const char *fmt, ...) __printflike(7, 8);
387 void destroy_autoclone_dev(cdev_t dev, struct devfs_bitmap *bitmap);
388 void sync_devs(void);