Merge branch 'vendor/TCPDUMP' (early part)
[dragonfly.git] / sys / sys / device.h
1 /*
2  * Copyright (c) 2003,2004,2007 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
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
16  *    distribution.
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.
20  * 
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
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sys/sys/device.h,v 1.11 2007/05/17 03:02:00 dillon Exp $
35  */
36
37 #ifndef _SYS_DEVICE_H_
38 #define _SYS_DEVICE_H_
39
40 #ifndef _SYS_TYPES_H_
41 #include <sys/types.h>
42 #endif
43 #ifndef _SYS_TREE_H_
44 #include <sys/tree.h>
45 #endif
46 #ifndef _SYS_SYSLINK_RPC_H_
47 #include <sys/syslink_rpc.h>
48 #endif
49
50 struct cdev;
51
52 /*
53  * This structure is at the base of every device args structure
54  */
55 struct dev_generic_args {
56         struct syslink_desc *a_desc;
57         struct cdev *a_dev;
58 };
59
60 typedef struct dev_generic_args dev_default_args;
61
62 /*
63  * int d_open(cdev_t dev, int oflags, int devtype, struct ucred *cred)
64  */
65 struct dev_open_args {
66         struct dev_generic_args a_head;
67         int             a_oflags;
68         int             a_devtype;
69         struct ucred    *a_cred;
70 };
71
72 /*
73  * int d_close(cdev_t dev, int fflag, int devtype)
74  */
75 struct dev_close_args {
76         struct dev_generic_args a_head;
77         int             a_fflag;
78         int             a_devtype;
79 };
80
81 /*
82  * int d_read(cdev_t dev, struct uio *uio, int ioflag)
83  */
84 struct dev_read_args {
85         struct dev_generic_args a_head;
86         struct uio      *a_uio;
87         int             a_ioflag;
88 };
89
90 /*
91  * int d_write(cdev_t dev, struct uio *uio, int ioflag)
92  */
93 struct dev_write_args {
94         struct dev_generic_args a_head;
95         struct uio      *a_uio;
96         int             a_ioflag;
97 };
98
99 /*
100  * int d_ioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag,
101  *             struct ucred *cred)
102  */
103 struct dev_ioctl_args {
104         struct dev_generic_args a_head;
105         u_long          a_cmd;
106         caddr_t         a_data;
107         int             a_fflag;
108         struct ucred    *a_cred;
109 };
110
111 /*
112  * int d_poll(cdev_t dev, int events)
113  */
114 struct dev_poll_args {
115         struct dev_generic_args a_head;
116         int             a_events;
117 };
118
119 /*
120  * int d_mmap(cdev_t dev, vm_offset_t offset, int nprot)
121  */
122 struct dev_mmap_args {
123         struct dev_generic_args a_head;
124         vm_offset_t     a_offset;
125         int             a_nprot;
126         int             a_result;       /* page number */
127 };
128
129 /*
130  * void d_strategy(cdev_t dev, struct bio *bio)
131  */
132 struct dev_strategy_args {
133         struct dev_generic_args a_head;
134         struct bio      *a_bio;
135 };
136
137 /*
138  * void d_dump(cdev_t dev)
139  */
140 struct dev_dump_args {
141         struct dev_generic_args a_head;
142         u_int64_t       a_count;
143         u_int64_t       a_blkno;
144         u_int           a_secsize;
145 };
146
147 /*
148  * int d_psize(cdev_t dev)
149  */
150 struct dev_psize_args {
151         struct dev_generic_args a_head;
152         int64_t         a_result;
153 };
154
155 /*
156  * int d_kqfilter(cdev_t dev, struct knote *kn)
157  */
158 struct dev_kqfilter_args {
159         struct dev_generic_args a_head;
160         struct knote    *a_kn;
161         int             a_result;
162 };
163
164 struct dev_clone_args {
165         struct dev_generic_args a_head;
166 };
167
168 /*
169  * Typedefs to help drivers declare the driver routines and such
170  */
171 typedef int d_default_t (struct dev_generic_args *ap);
172 typedef int d_open_t (struct dev_open_args *ap);
173 typedef int d_close_t (struct dev_close_args *ap);
174 typedef int d_read_t (struct dev_read_args *ap);
175 typedef int d_write_t (struct dev_write_args *ap);
176 typedef int d_ioctl_t (struct dev_ioctl_args *ap);
177 typedef int d_poll_t (struct dev_poll_args *ap);
178 typedef int d_mmap_t (struct dev_mmap_args *ap);
179 typedef int d_strategy_t (struct dev_strategy_args *ap);
180 typedef int d_dump_t (struct dev_dump_args *ap);
181 typedef int d_psize_t (struct dev_psize_args *ap);
182 typedef int d_kqfilter_t (struct dev_kqfilter_args *ap);
183 typedef int d_clone_t (struct dev_clone_args *ap);
184
185 /*
186  * Character device switch table.
187  *
188  * NOTE: positions are hard coded for static structure initialization.
189  */
190 struct dev_ops {
191         struct {
192                 const char      *name;  /* base name, e.g. 'da' */
193                 int             maj;    /* major device number */
194                 u_int           flags;  /* D_XXX flags */
195                 void            *data;  /* custom driver data */
196                 int             refs;   /* ref count */
197         } head;
198
199 #define dev_ops_first_field     d_default
200         d_default_t     *d_default;
201         d_open_t        *d_open;
202         d_close_t       *d_close;
203         d_read_t        *d_read;
204         d_write_t       *d_write;
205         d_ioctl_t       *d_ioctl;
206         d_poll_t        *d_poll;
207         d_mmap_t        *d_mmap;
208         d_strategy_t    *d_strategy;
209         d_dump_t        *d_dump;
210         d_psize_t       *d_psize;
211         d_kqfilter_t    *d_kqfilter;
212         d_clone_t       *d_clone;       /* clone from base dev_ops */
213 #define dev_ops_last_field      d_clone
214 };
215
216 /*
217  * Types for d_flags.
218  */
219 #define D_TAPE          0x0001
220 #define D_DISK          0x0002
221 #define D_TTY           0x0004
222 #define D_MEM           0x0008
223
224 #define D_TYPEMASK      0xffff
225
226 /*
227  * Flags for d_flags.
228  */
229 #define D_MEMDISK       0x00010000      /* memory type disk */
230 #define D_NAGGED        0x00020000      /* nagged about missing make_dev() */
231 #define D_CANFREE       0x00040000      /* can free blocks */
232 #define D_TRACKCLOSE    0x00080000      /* track all closes */
233 #define D_MASTER        0x00100000      /* used by pty/tty code */
234 #define D_KQFILTER      0x00200000      /* has kqfilter entry */
235
236 /*
237  * A union of all possible argument structures.
238  */
239 union dev_args_union {
240         struct dev_generic_args du_head;
241         struct dev_open_args    du_open;
242         struct dev_close_args   du_close;
243         struct dev_read_args    du_read;
244         struct dev_write_args   du_write;
245         struct dev_ioctl_args   du_ioctl;
246         struct dev_poll_args    du_poll;
247         struct dev_mmap_args    du_mmap;
248         struct dev_strategy_args du_strategy;
249         struct dev_dump_args    du_dump;
250         struct dev_psize_args   du_psize;
251         struct dev_kqfilter_args du_kqfilter;
252         struct dev_clone_args   du_clone;
253 };
254
255 /*
256  * Linking structure for mask/match registration
257  */
258 struct dev_ops_link {
259         struct dev_ops_link *next;
260         u_int           mask;
261         u_int           match;
262         struct dev_ops  *ops;
263 };
264
265 struct dev_ops_maj {
266         RB_ENTRY(dev_ops_maj) rbnode; /* red-black tree of major nums */
267         struct dev_ops_link *link;
268         int             maj;
269 };
270
271 RB_HEAD(dev_ops_rb_tree, dev_ops_maj);
272 RB_PROTOTYPE2(dev_ops_rb_tree, dev_ops_maj, rbnode, rb_dev_ops_compare, int);
273
274 #ifdef _KERNEL
275
276 extern struct dev_ops dead_dev_ops;
277
278 struct disk;
279
280 int dev_dopen(cdev_t dev, int oflags, int devtype, struct ucred *cred);
281 int dev_dclose(cdev_t dev, int fflag, int devtype);
282 void dev_dstrategy(cdev_t dev, struct bio *bio);
283 void dev_dstrategy_chain(cdev_t dev, struct bio *bio);
284 int dev_dioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag,
285                 struct ucred *cred);
286 int dev_ddump(cdev_t dev);
287 int64_t dev_dpsize(cdev_t dev);
288 int dev_dread(cdev_t dev, struct uio *uio, int ioflag);
289 int dev_dwrite(cdev_t dev, struct uio *uio, int ioflag);
290 int dev_dpoll(cdev_t dev, int events);
291 int dev_dkqfilter(cdev_t dev, struct knote *kn);
292 int dev_dmmap(cdev_t dev, vm_offset_t offset, int nprot);
293 int dev_dclone(cdev_t dev);
294
295 int dev_drefs(cdev_t dev);
296 const char *dev_dname(cdev_t dev);
297 int dev_dmaj(cdev_t dev);
298 int dev_dflags(cdev_t dev);
299 int dev_doperate(struct dev_generic_args *ap);
300 int dev_doperate_ops(struct dev_ops *, struct dev_generic_args *ap);
301
302 d_default_t     nodefault;
303 d_open_t        noopen;
304 d_close_t       noclose;
305 d_read_t        noread;
306 d_write_t       nowrite;
307 d_ioctl_t       noioctl;
308 d_poll_t        nopoll;
309 d_mmap_t        nommap;
310 d_strategy_t    nostrategy;
311 d_dump_t        nodump;
312 d_psize_t       nopsize;
313 d_kqfilter_t    nokqfilter;
314 d_clone_t       noclone;
315
316 d_open_t        nullopen;
317 d_close_t       nullclose;
318
319 extern struct syslink_desc dev_default_desc;
320 extern struct syslink_desc dev_open_desc;
321 extern struct syslink_desc dev_close_desc;
322 extern struct syslink_desc dev_read_desc;
323 extern struct syslink_desc dev_write_desc;
324 extern struct syslink_desc dev_ioctl_desc;
325 extern struct syslink_desc dev_dump_desc;
326 extern struct syslink_desc dev_psize_desc;
327 extern struct syslink_desc dev_poll_desc;
328 extern struct syslink_desc dev_mmap_desc;
329 extern struct syslink_desc dev_strategu_desc;
330 extern struct syslink_desc dev_kqfilter_desc;
331 extern struct syslink_desc dev_clone_desc;
332
333 void compile_dev_ops(struct dev_ops *);
334 int dev_ops_scan(int (*callback)(struct dev_ops *, void *), void *arg);
335 int dev_ops_add(struct dev_ops *, u_int mask, u_int match);
336 int dev_ops_remove(struct dev_ops *, u_int mask, u_int match);
337 void dev_ops_release(struct dev_ops *);
338 struct dev_ops *dev_ops_add_override(cdev_t, struct dev_ops *, u_int, u_int);
339 void dev_ops_remove_override(struct dev_ops *ops, u_int mask, u_int match);
340
341 struct dev_ops *dev_ops_intercept(cdev_t, struct dev_ops *);
342 void dev_ops_restore(cdev_t, struct dev_ops *);
343 struct dev_ops *dev_ops_get(int x, int y);
344
345 void destroy_all_devs(struct dev_ops *, u_int mask, u_int match);
346 cdev_t make_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
347                 int perms, const char *fmt, ...) __printflike(6, 7);
348 cdev_t make_adhoc_dev (struct dev_ops *ops, int minor);
349
350 #endif
351
352 #endif
353