Merge branch 'vendor/DHCPCD'
[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
35 #ifndef _SYS_DEVICE_H_
36 #define _SYS_DEVICE_H_
37
38 #ifdef _KERNEL
39 #ifndef _SYS_TYPES_H_
40 #include <sys/types.h>
41 #endif
42 #ifndef _SYS_TREE_H_
43 #include <sys/tree.h>
44 #endif
45 #ifndef _SYS_SYSLINK_RPC_H_
46 #include <sys/syslink_rpc.h>
47 #endif
48
49 struct cdev;
50 struct ucred;
51 struct devfs_bitmap;
52 struct vm_page;
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         struct file     *a_fp;
73 };
74
75 /*
76  * int d_close(cdev_t dev, int fflag, int devtype)
77  */
78 struct dev_close_args {
79         struct dev_generic_args a_head;
80         int             a_fflag;
81         int             a_devtype;
82         struct file     *a_fp;
83 };
84
85 /*
86  * int d_read(cdev_t dev, struct uio *uio, int ioflag)
87  */
88 struct dev_read_args {
89         struct dev_generic_args a_head;
90         struct uio      *a_uio;
91         int             a_ioflag;
92         struct file     *a_fp;
93 };
94
95 /*
96  * int d_write(cdev_t dev, struct uio *uio, int ioflag)
97  */
98 struct dev_write_args {
99         struct dev_generic_args a_head;
100         struct uio      *a_uio;
101         int             a_ioflag;
102         struct file     *a_fp;
103 };
104
105 /*
106  * int d_ioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag,
107  *             struct ucred *cred, struct sysmsg *msg)
108  */
109 struct dev_ioctl_args {
110         struct dev_generic_args a_head;
111         u_long          a_cmd;
112         caddr_t         a_data;
113         int             a_fflag;
114         struct ucred    *a_cred;
115         struct sysmsg   *a_sysmsg;
116         struct file     *a_fp;
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         int64_t         a_result;       /* page number */
127         struct file     *a_fp;
128 };
129
130 /*
131  * int d_mmap_single(cdev_t dev, vm_ooffset_t *offset, vm_size_t size,
132  *                   struct vm_object **object, int nprot)
133  */
134 struct dev_mmap_single_args {
135         struct dev_generic_args a_head;
136         vm_ooffset_t    *a_offset;
137         vm_size_t               a_size;
138         struct vm_object **a_object;
139         int             a_nprot;
140         struct file     *a_fp;
141 };
142
143 /*
144  * void d_strategy(cdev_t dev, struct bio *bio)
145  */
146 struct dev_strategy_args {
147         struct dev_generic_args a_head;
148         struct bio      *a_bio;
149 };
150
151 /*
152  * void d_dump(cdev_t dev, void *virtual, vm_offset_t physical,
153                 off_t offset, size_t length)
154  */
155 struct dev_dump_args {
156         struct dev_generic_args a_head;
157         u_int64_t       a_count;
158         u_int64_t       a_blkno;
159         u_int           a_secsize;
160         void            *a_virtual;
161         vm_offset_t     a_physical;
162         off_t           a_offset;
163         size_t          a_length;
164 };
165
166 /*
167  * int d_psize(cdev_t dev)
168  */
169 struct dev_psize_args {
170         struct dev_generic_args a_head;
171         int64_t         a_result;
172 };
173
174 /*
175  * int d_kqfilter(cdev_t dev, struct knote *kn)
176  */
177 struct dev_kqfilter_args {
178         struct dev_generic_args a_head;
179         struct knote    *a_kn;
180         int             a_result;
181         struct file     *a_fp;
182 };
183
184 /*
185  * int d_clone(cdev_t dev);
186  */
187 struct dev_clone_args {
188         struct dev_generic_args a_head;
189
190         struct cdev     *a_dev;
191         const char      *a_name;
192         size_t          a_namelen;
193         struct ucred    *a_cred;
194         int             a_mode;
195 };
196
197 /*
198  * int d_revoke(cdev_t dev)
199  */
200 struct dev_revoke_args {
201         struct dev_generic_args a_head;
202 };
203
204 /*
205  * Typedefs to help drivers declare the driver routines and such
206  */
207 typedef int d_default_t (struct dev_generic_args *ap);
208 typedef int d_open_t (struct dev_open_args *ap);
209 typedef int d_close_t (struct dev_close_args *ap);
210 typedef int d_read_t (struct dev_read_args *ap);
211 typedef int d_write_t (struct dev_write_args *ap);
212 typedef int d_ioctl_t (struct dev_ioctl_args *ap);
213 typedef int d_mmap_t (struct dev_mmap_args *ap);
214 typedef int d_mmap_single_t (struct dev_mmap_single_args *ap);
215 typedef int d_strategy_t (struct dev_strategy_args *ap);
216 typedef int d_dump_t (struct dev_dump_args *ap);
217 typedef int d_psize_t (struct dev_psize_args *ap);
218 typedef int d_kqfilter_t (struct dev_kqfilter_args *ap);
219 typedef int d_clone_t (struct dev_clone_args *ap);
220 typedef int d_revoke_t (struct dev_revoke_args *ap);
221
222 /*
223  * Character device switch table.
224  *
225  * NOTE: positions are hard coded for static structure initialization.
226  */
227 struct dev_ops {
228         struct {
229                 const char      *name;  /* base name, e.g. 'da' */
230                 int              maj;   /* major device number */
231                 u_int            flags; /* D_XXX flags */
232                 void            *data;  /* custom driver data */
233                 int              refs;  /* ref count */
234                 int              id;
235         } head;
236
237 #define dev_ops_first_field     d_default
238         d_default_t     *d_default;
239         d_open_t        *d_open;
240         d_close_t       *d_close;
241         d_read_t        *d_read;
242         d_write_t       *d_write;
243         d_ioctl_t       *d_ioctl;
244         d_mmap_t        *d_mmap;
245         d_mmap_single_t *d_mmap_single;
246         d_strategy_t    *d_strategy;
247         d_dump_t        *d_dump;
248         d_psize_t       *d_psize;
249         d_kqfilter_t    *d_kqfilter;
250         d_clone_t       *d_clone;       /* clone from base dev_ops */
251         d_revoke_t      *d_revoke;
252         int (*d_uksmap)(struct cdev *dev, struct vm_page *fake);
253 #define dev_ops_last_field      d_uksmap
254 };
255 #endif /* _KERNEL */
256
257 /*
258  * Types for d_flags.
259  */
260 #define D_TAPE          0x0001
261 #define D_DISK          0x0002
262 #define D_TTY           0x0004
263 #define D_MEM           0x0008
264
265 #define D_TYPEMASK      0xffff
266 #define D_SEEKABLE      (D_TAPE | D_DISK | D_MEM)
267
268 /*
269  * Flags for d_flags.
270  *
271  * D_NOEMERGPGR         Indicates complex layering, the emergency pager
272  *                      should skip buffers related to such devices.
273  */
274 #ifdef _KERNEL
275 #define D_MEMDISK       0x00010000      /* memory type disk */
276 #define D_UNUSED17      0x00020000      /* was: nagged about missing make_dev() */
277 #define D_CANFREE       0x00040000      /* can free blocks */
278 #define D_TRACKCLOSE    0x00080000      /* track all closes */
279 #define D_MASTER        0x00100000      /* used by pty/tty code */
280 #define D_NOEMERGPGR    0x00200000      /* too complex for emergency pager */
281 #define D_MPSAFE        0x00400000      /* all dev_d*() calls are MPSAFE */
282 #define D_KVABIO        0x00800000      /* device support KVABIO API */
283 #define D_QUICK         0x01000000      /* no fancy open/close support needed*/
284 #endif
285
286 /*
287  * A union of all possible argument structures.
288  */
289 #if 0
290 union dev_args_union {
291         struct dev_generic_args du_head;
292         struct dev_open_args    du_open;
293         struct dev_close_args   du_close;
294         struct dev_read_args    du_read;
295         struct dev_write_args   du_write;
296         struct dev_ioctl_args   du_ioctl;
297         struct dev_mmap_args    du_mmap;
298         struct dev_strategy_args du_strategy;
299         struct dev_dump_args    du_dump;
300         struct dev_psize_args   du_psize;
301         struct dev_kqfilter_args du_kqfilter;
302         struct dev_clone_args   du_clone;
303 };
304 #endif
305
306 #ifdef _KERNEL
307 /*
308  * Linking structure for mask/match registration
309  */
310 struct dev_ops_link {
311         struct dev_ops_link *next;
312         u_int           mask;
313         u_int           match;
314         struct dev_ops  *ops;
315 };
316
317 struct dev_ops_maj {
318         RB_ENTRY(dev_ops_maj) rbnode; /* red-black tree of major nums */
319         struct dev_ops_link *link;
320         int             maj;
321 };
322
323 RB_HEAD(dev_ops_rb_tree, dev_ops_maj);
324 RB_PROTOTYPE2(dev_ops_rb_tree, dev_ops_maj, rbnode, rb_dev_ops_compare, int);
325 #endif /* _KERNEL */
326
327 #ifdef _KERNEL
328
329 extern struct dev_ops dead_dev_ops;
330
331 struct disk;
332 struct sysmsg;
333
334 int dev_dopen(cdev_t dev, int oflags, int devtype, struct ucred *cred, struct file *fp);
335 int dev_dclose(cdev_t dev, int fflag, int devtype, struct file *fp);
336 void dev_dstrategy(cdev_t dev, struct bio *bio);
337 void dev_dstrategy_chain(cdev_t dev, struct bio *bio);
338 int dev_dioctl(cdev_t dev, u_long cmd, caddr_t data,
339                 int fflag, struct ucred *cred, struct sysmsg *msg, struct file *fp);
340 int dev_ddump(cdev_t dev, void *virtual, vm_offset_t physical, off_t offset,
341     size_t length);
342 int64_t dev_dpsize(cdev_t dev);
343 int dev_dread(cdev_t dev, struct uio *uio, int ioflag, struct file *fp);
344 int dev_dwrite(cdev_t dev, struct uio *uio, int ioflag, struct file *fp);
345 int dev_dkqfilter(cdev_t dev, struct knote *kn, struct file *fp);
346 int64_t dev_dmmap(cdev_t dev, vm_offset_t offset, int nprot, struct file *fp);
347 int dev_dmmap_single(cdev_t dev, vm_ooffset_t *offset, vm_size_t size,
348                         struct vm_object **object, int nprot, struct file *fp);
349 int dev_dclone(cdev_t dev);
350 int dev_drevoke(cdev_t dev);
351
352 int dev_drefs(cdev_t dev);
353 const char *dev_dname(cdev_t dev);
354 int dev_dmaj(cdev_t dev);
355 int dev_dflags(cdev_t dev);
356 int dev_doperate(struct dev_generic_args *ap);
357 int dev_doperate_ops(struct dev_ops *, struct dev_generic_args *ap);
358
359 d_open_t        nullopen;
360 d_close_t       nullclose;
361
362 extern struct syslink_desc dev_default_desc;
363 extern struct syslink_desc dev_open_desc;
364 extern struct syslink_desc dev_close_desc;
365 extern struct syslink_desc dev_read_desc;
366 extern struct syslink_desc dev_write_desc;
367 extern struct syslink_desc dev_ioctl_desc;
368 extern struct syslink_desc dev_dump_desc;
369 extern struct syslink_desc dev_psize_desc;
370 extern struct syslink_desc dev_mmap_desc;
371 extern struct syslink_desc dev_mmap_single_desc;
372 extern struct syslink_desc dev_strategy_desc;
373 extern struct syslink_desc dev_kqfilter_desc;
374 extern struct syslink_desc dev_clone_desc;
375
376 void compile_dev_ops(struct dev_ops *);
377 int dev_ops_remove_all(struct dev_ops *ops);
378 int dev_ops_remove_minor(struct dev_ops *ops, int minor);
379 struct dev_ops *dev_ops_intercept(cdev_t, struct dev_ops *);
380 void dev_ops_restore(cdev_t, struct dev_ops *);
381
382 #define MAKEDEV_MINNBUF 14
383 char  *makedev_unit_b32(char *nbuf, uintmax_t num);
384 cdev_t make_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
385                 int perms, const char *fmt, ...) __printflike(6, 7);
386 cdev_t make_dev_covering(struct dev_ops *ops,  struct dev_ops *bops, int minor,
387             uid_t uid, gid_t gid, int perms, const char *fmt, ...) __printflike(7, 8);
388 cdev_t make_only_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
389                 int perms, const char *fmt, ...) __printflike(6, 7);
390 cdev_t make_only_dev_covering(struct dev_ops *ops, struct dev_ops *bops, int minor,
391     uid_t uid, gid_t gid, int perms, const char *fmt, ...) __printflike(7,8);
392 cdev_t make_only_devfs_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
393            int perms, const char *fmt, ...) __printflike(6, 7);
394 void destroy_only_dev(cdev_t dev);
395 int make_dev_alias(cdev_t target, const char *fmt, ...) __printflike(2, 3);
396 int destroy_dev_alias(cdev_t target, const char *fmt, ...) __printflike(2, 3);
397 cdev_t make_autoclone_dev(struct dev_ops *ops, struct devfs_bitmap *bitmap,
398            d_clone_t *nhandler, uid_t uid, gid_t gid, int perms,
399            const char *fmt, ...) __printflike(7, 8);
400 void destroy_autoclone_dev(cdev_t dev, struct devfs_bitmap *bitmap);
401 void sync_devs(void);
402
403 #endif
404
405 #endif
406