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