Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[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 struct ucred;
52 struct devfs_bitmap;
53
54 /*
55  * This structure is at the base of every device args structure
56  */
57 struct dev_generic_args {
58         struct syslink_desc *a_desc;
59         struct cdev *a_dev;
60 };
61
62 typedef struct dev_generic_args dev_default_args;
63
64 /*
65  * int d_open(cdev_t dev, int oflags, int devtype, struct ucred *cred)
66  */
67 struct dev_open_args {
68         struct dev_generic_args a_head;
69         int             a_oflags;
70         int             a_devtype;
71         struct ucred    *a_cred;
72 };
73
74 /*
75  * int d_close(cdev_t dev, int fflag, int devtype)
76  */
77 struct dev_close_args {
78         struct dev_generic_args a_head;
79         int             a_fflag;
80         int             a_devtype;
81 };
82
83 /*
84  * int d_read(cdev_t dev, struct uio *uio, int ioflag)
85  */
86 struct dev_read_args {
87         struct dev_generic_args a_head;
88         struct uio      *a_uio;
89         int             a_ioflag;
90 };
91
92 /*
93  * int d_write(cdev_t dev, struct uio *uio, int ioflag)
94  */
95 struct dev_write_args {
96         struct dev_generic_args a_head;
97         struct uio      *a_uio;
98         int             a_ioflag;
99 };
100
101 /*
102  * int d_ioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag,
103  *             struct ucred *cred)
104  */
105 struct dev_ioctl_args {
106         struct dev_generic_args a_head;
107         u_long          a_cmd;
108         caddr_t         a_data;
109         int             a_fflag;
110         struct ucred    *a_cred;
111 };
112
113 /*
114  * int d_poll(cdev_t dev, int events)
115  */
116 struct dev_poll_args {
117         struct dev_generic_args a_head;
118         int             a_events;
119 };
120
121 /*
122  * int d_mmap(cdev_t dev, vm_offset_t offset, int nprot)
123  */
124 struct dev_mmap_args {
125         struct dev_generic_args a_head;
126         vm_offset_t     a_offset;
127         int             a_nprot;
128         int             a_result;       /* page number */
129 };
130
131 /*
132  * void d_strategy(cdev_t dev, struct bio *bio)
133  */
134 struct dev_strategy_args {
135         struct dev_generic_args a_head;
136         struct bio      *a_bio;
137 };
138
139 /*
140  * void d_dump(cdev_t dev)
141  */
142 struct dev_dump_args {
143         struct dev_generic_args a_head;
144         u_int64_t       a_count;
145         u_int64_t       a_blkno;
146         u_int           a_secsize;
147 };
148
149 /*
150  * int d_psize(cdev_t dev)
151  */
152 struct dev_psize_args {
153         struct dev_generic_args a_head;
154         int64_t         a_result;
155 };
156
157 /*
158  * int d_kqfilter(cdev_t dev, struct knote *kn)
159  */
160 struct dev_kqfilter_args {
161         struct dev_generic_args a_head;
162         struct knote    *a_kn;
163         int             a_result;
164 };
165
166 /*
167  * int d_clone(cdev_t dev);
168  */
169 struct dev_clone_args {
170         struct dev_generic_args a_head;
171
172         struct cdev     *a_dev;
173         const char      *a_name;
174         size_t          a_namelen;
175         struct ucred    *a_cred;
176         int             a_mode;
177 };
178
179 /*
180  * int d_revoke(cdev_t dev)
181  */
182 struct dev_revoke_args {
183         struct dev_generic_args a_head;
184 };
185
186 /*
187  * Typedefs to help drivers declare the driver routines and such
188  */
189 typedef int d_default_t (struct dev_generic_args *ap);
190 typedef int d_open_t (struct dev_open_args *ap);
191 typedef int d_close_t (struct dev_close_args *ap);
192 typedef int d_read_t (struct dev_read_args *ap);
193 typedef int d_write_t (struct dev_write_args *ap);
194 typedef int d_ioctl_t (struct dev_ioctl_args *ap);
195 typedef int d_poll_t (struct dev_poll_args *ap);
196 typedef int d_mmap_t (struct dev_mmap_args *ap);
197 typedef int d_strategy_t (struct dev_strategy_args *ap);
198 typedef int d_dump_t (struct dev_dump_args *ap);
199 typedef int d_psize_t (struct dev_psize_args *ap);
200 typedef int d_kqfilter_t (struct dev_kqfilter_args *ap);
201 typedef int d_clone_t (struct dev_clone_args *ap);
202 typedef int d_revoke_t (struct dev_revoke_args *ap);
203
204 /*
205  * Character device switch table.
206  *
207  * NOTE: positions are hard coded for static structure initialization.
208  */
209 struct dev_ops {
210         struct {
211                 const char      *name;  /* base name, e.g. 'da' */
212                 int                     maj;    /* major device number */
213                 u_int           flags;  /* D_XXX flags */
214                 void            *data;  /* custom driver data */
215                 int                     refs;   /* ref count */
216                 int                     id;
217         } head;
218
219 #define dev_ops_first_field     d_default
220         d_default_t     *d_default;
221         d_open_t        *d_open;
222         d_close_t       *d_close;
223         d_read_t        *d_read;
224         d_write_t       *d_write;
225         d_ioctl_t       *d_ioctl;
226         d_poll_t        *d_poll;
227         d_mmap_t        *d_mmap;
228         d_strategy_t    *d_strategy;
229         d_dump_t        *d_dump;
230         d_psize_t       *d_psize;
231         d_kqfilter_t    *d_kqfilter;
232         d_clone_t       *d_clone;       /* clone from base dev_ops */
233         d_revoke_t      *d_revoke;
234 #define dev_ops_last_field      d_revoke
235 };
236
237 /*
238  * Types for d_flags.
239  */
240 #define D_TAPE          0x0001
241 #define D_DISK          0x0002
242 #define D_TTY           0x0004
243 #define D_MEM           0x0008
244
245 #define D_TYPEMASK      0xffff
246 #define D_SEEKABLE      (D_TAPE | D_DISK | D_MEM)
247
248 /*
249  * Flags for d_flags.
250  */
251 #define D_MEMDISK       0x00010000      /* memory type disk */
252 #define D_NAGGED        0x00020000      /* nagged about missing make_dev() */
253 #define D_CANFREE       0x00040000      /* can free blocks */
254 #define D_TRACKCLOSE    0x00080000      /* track all closes */
255 #define D_MASTER        0x00100000      /* used by pty/tty code */
256 #define D_KQFILTER      0x00200000      /* has kqfilter entry */
257
258 /*
259  * A union of all possible argument structures.
260  */
261 union dev_args_union {
262         struct dev_generic_args du_head;
263         struct dev_open_args    du_open;
264         struct dev_close_args   du_close;
265         struct dev_read_args    du_read;
266         struct dev_write_args   du_write;
267         struct dev_ioctl_args   du_ioctl;
268         struct dev_poll_args    du_poll;
269         struct dev_mmap_args    du_mmap;
270         struct dev_strategy_args du_strategy;
271         struct dev_dump_args    du_dump;
272         struct dev_psize_args   du_psize;
273         struct dev_kqfilter_args du_kqfilter;
274         struct dev_clone_args   du_clone;
275 };
276
277 /*
278  * Linking structure for mask/match registration
279  */
280 struct dev_ops_link {
281         struct dev_ops_link *next;
282         u_int           mask;
283         u_int           match;
284         struct dev_ops  *ops;
285 };
286
287 struct dev_ops_maj {
288         RB_ENTRY(dev_ops_maj) rbnode; /* red-black tree of major nums */
289         struct dev_ops_link *link;
290         int             maj;
291 };
292
293 RB_HEAD(dev_ops_rb_tree, dev_ops_maj);
294 RB_PROTOTYPE2(dev_ops_rb_tree, dev_ops_maj, rbnode, rb_dev_ops_compare, int);
295
296 #ifdef _KERNEL
297
298 extern struct dev_ops dead_dev_ops;
299
300 struct disk;
301
302 int dev_dopen(cdev_t dev, int oflags, int devtype, struct ucred *cred);
303 int dev_dclose(cdev_t dev, int fflag, int devtype);
304 void dev_dstrategy(cdev_t dev, struct bio *bio);
305 void dev_dstrategy_chain(cdev_t dev, struct bio *bio);
306 int dev_dioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag,
307                 struct ucred *cred);
308 int dev_ddump(cdev_t dev);
309 int64_t dev_dpsize(cdev_t dev);
310 int dev_dread(cdev_t dev, struct uio *uio, int ioflag);
311 int dev_dwrite(cdev_t dev, struct uio *uio, int ioflag);
312 int dev_dpoll(cdev_t dev, int events);
313 int dev_dkqfilter(cdev_t dev, struct knote *kn);
314 int dev_dmmap(cdev_t dev, vm_offset_t offset, int nprot);
315 int dev_dclone(cdev_t dev);
316 int dev_drevoke(cdev_t dev);
317
318 int dev_drefs(cdev_t dev);
319 const char *dev_dname(cdev_t dev);
320 int dev_dmaj(cdev_t dev);
321 int dev_dflags(cdev_t dev);
322 int dev_doperate(struct dev_generic_args *ap);
323 int dev_doperate_ops(struct dev_ops *, struct dev_generic_args *ap);
324
325 d_default_t     nodefault;
326 d_open_t        noopen;
327 d_close_t       noclose;
328 d_read_t        noread;
329 d_write_t       nowrite;
330 d_ioctl_t       noioctl;
331 d_poll_t        nopoll;
332 d_mmap_t        nommap;
333 d_strategy_t    nostrategy;
334 d_dump_t        nodump;
335 d_psize_t       nopsize;
336 d_kqfilter_t    nokqfilter;
337 d_clone_t       noclone;
338 d_revoke_t      norevoke;
339
340 d_open_t        nullopen;
341 d_close_t       nullclose;
342
343 extern struct syslink_desc dev_default_desc;
344 extern struct syslink_desc dev_open_desc;
345 extern struct syslink_desc dev_close_desc;
346 extern struct syslink_desc dev_read_desc;
347 extern struct syslink_desc dev_write_desc;
348 extern struct syslink_desc dev_ioctl_desc;
349 extern struct syslink_desc dev_dump_desc;
350 extern struct syslink_desc dev_psize_desc;
351 extern struct syslink_desc dev_poll_desc;
352 extern struct syslink_desc dev_mmap_desc;
353 extern struct syslink_desc dev_strategu_desc;
354 extern struct syslink_desc dev_kqfilter_desc;
355 extern struct syslink_desc dev_clone_desc;
356
357 void compile_dev_ops(struct dev_ops *);
358 int dev_ops_remove_all(struct dev_ops *ops);
359 int dev_ops_remove_minor(struct dev_ops *ops, int minor);
360 struct dev_ops *dev_ops_intercept(cdev_t, struct dev_ops *);
361 void dev_ops_restore(cdev_t, struct dev_ops *);
362
363 cdev_t make_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
364                 int perms, const char *fmt, ...) __printflike(6, 7);
365 cdev_t make_only_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
366                 int perms, const char *fmt, ...) __printflike(6, 7);
367 cdev_t make_only_devfs_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
368                 int perms, const char *fmt, ...) __printflike(6, 7);
369 void destroy_only_dev(cdev_t dev);
370 int make_dev_alias(cdev_t target, const char *fmt, ...);
371 cdev_t make_autoclone_dev(struct dev_ops *ops, struct devfs_bitmap *bitmap,
372                 d_clone_t *nhandler, uid_t uid, gid_t gid, int perms, const char *fmt, ...);
373 void destroy_autoclone_dev(cdev_t dev, struct devfs_bitmap *bitmap);
374 void sync_devs(void);
375
376 #endif
377
378 #endif
379