devfs - Add a destroy_dev_alias, remove hotplug
[dragonfly.git] / sys / sys / device.h
CommitLineData
335dda38 1/*
0e9b9130 2 * Copyright (c) 2003,2004,2007 The DragonFly Project. All rights reserved.
07dfa375 3 *
8c10bfcf
MD
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
07dfa375 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:
07dfa375 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.
07dfa375 20 *
8c10bfcf
MD
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.
07dfa375 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;
b96f3782 52struct devfs_bitmap;
b13267a5 53
335dda38 54/*
fef8985e 55 * This structure is at the base of every device args structure
335dda38 56 */
fef8985e
MD
57struct dev_generic_args {
58 struct syslink_desc *a_desc;
b13267a5 59 struct cdev *a_dev;
335dda38
MD
60};
61
fef8985e
MD
62typedef struct dev_generic_args dev_default_args;
63
335dda38 64/*
b13267a5 65 * int d_open(cdev_t dev, int oflags, int devtype, struct ucred *cred)
335dda38 66 */
fef8985e
MD
67struct dev_open_args {
68 struct dev_generic_args a_head;
69 int a_oflags;
70 int a_devtype;
71 struct ucred *a_cred;
335dda38
MD
72};
73
74/*
b13267a5 75 * int d_close(cdev_t dev, int fflag, int devtype)
335dda38 76 */
fef8985e
MD
77struct dev_close_args {
78 struct dev_generic_args a_head;
79 int a_fflag;
80 int a_devtype;
335dda38
MD
81};
82
83/*
b13267a5 84 * int d_read(cdev_t dev, struct uio *uio, int ioflag)
335dda38 85 */
fef8985e
MD
86struct dev_read_args {
87 struct dev_generic_args a_head;
88 struct uio *a_uio;
89 int a_ioflag;
335dda38
MD
90};
91
92/*
b13267a5 93 * int d_write(cdev_t dev, struct uio *uio, int ioflag)
335dda38 94 */
fef8985e
MD
95struct dev_write_args {
96 struct dev_generic_args a_head;
97 struct uio *a_uio;
98 int a_ioflag;
335dda38
MD
99};
100
101/*
b13267a5 102 * int d_ioctl(cdev_t dev, u_long cmd, caddr_t data, int fflag,
87baaf0c 103 * struct ucred *cred, struct sysmsg *msg)
335dda38 104 */
fef8985e
MD
105struct 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;
87baaf0c 111 struct sysmsg *a_sysmsg;
335dda38
MD
112};
113
114/*
b13267a5 115 * int d_mmap(cdev_t dev, vm_offset_t offset, int nprot)
335dda38 116 */
fef8985e
MD
117struct dev_mmap_args {
118 struct dev_generic_args a_head;
119 vm_offset_t a_offset;
120 int a_nprot;
121 int a_result; /* page number */
335dda38
MD
122};
123
124/*
b13267a5 125 * void d_strategy(cdev_t dev, struct bio *bio)
335dda38 126 */
fef8985e
MD
127struct dev_strategy_args {
128 struct dev_generic_args a_head;
129 struct bio *a_bio;
335dda38
MD
130};
131
132/*
b24cd69c
AH
133 * void d_dump(cdev_t dev, void *virtual, vm_offset_t physical,
134 off_t offset, size_t length)
335dda38 135 */
fef8985e
MD
136struct dev_dump_args {
137 struct dev_generic_args a_head;
e0fc5693
MD
138 u_int64_t a_count;
139 u_int64_t a_blkno;
fef8985e 140 u_int a_secsize;
b24cd69c
AH
141 void *a_virtual;
142 vm_offset_t a_physical;
143 off_t a_offset;
144 size_t a_length;
fef8985e
MD
145};
146
147/*
b13267a5 148 * int d_psize(cdev_t dev)
fef8985e
MD
149 */
150struct dev_psize_args {
151 struct dev_generic_args a_head;
e0fc5693 152 int64_t a_result;
335dda38
MD
153};
154
155/*
b13267a5 156 * int d_kqfilter(cdev_t dev, struct knote *kn)
335dda38 157 */
fef8985e
MD
158struct dev_kqfilter_args {
159 struct dev_generic_args a_head;
160 struct knote *a_kn;
161 int a_result;
162};
163
a32446b7
MD
164/*
165 * int d_clone(cdev_t dev);
166 */
fef8985e
MD
167struct dev_clone_args {
168 struct dev_generic_args a_head;
cd29885a 169
07dfa375
AH
170 struct cdev *a_dev;
171 const char *a_name;
172 size_t a_namelen;
173 struct ucred *a_cred;
174 int a_mode;
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);
fef8985e
MD
193typedef int d_mmap_t (struct dev_mmap_args *ap);
194typedef int d_strategy_t (struct dev_strategy_args *ap);
195typedef int d_dump_t (struct dev_dump_args *ap);
196typedef int d_psize_t (struct dev_psize_args *ap);
197typedef int d_kqfilter_t (struct dev_kqfilter_args *ap);
198typedef int d_clone_t (struct dev_clone_args *ap);
a32446b7 199typedef int d_revoke_t (struct dev_revoke_args *ap);
fef8985e
MD
200
201/*
202 * Character device switch table.
203 *
204 * NOTE: positions are hard coded for static structure initialization.
205 */
206struct dev_ops {
207 struct {
208 const char *name; /* base name, e.g. 'da' */
e94d6677
SW
209 int maj; /* major device number */
210 u_int flags; /* D_XXX flags */
7cbab9da 211 void *data; /* custom driver data */
e94d6677
SW
212 int refs; /* ref count */
213 int id;
fef8985e
MD
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;
fef8985e
MD
223 d_mmap_t *d_mmap;
224 d_strategy_t *d_strategy;
225 d_dump_t *d_dump;
226 d_psize_t *d_psize;
227 d_kqfilter_t *d_kqfilter;
228 d_clone_t *d_clone; /* clone from base dev_ops */
a32446b7
MD
229 d_revoke_t *d_revoke;
230#define dev_ops_last_field d_revoke
fef8985e
MD
231};
232
233/*
234 * Types for d_flags.
335dda38 235 */
fef8985e
MD
236#define D_TAPE 0x0001
237#define D_DISK 0x0002
238#define D_TTY 0x0004
239#define D_MEM 0x0008
240
241#define D_TYPEMASK 0xffff
c0885fab 242#define D_SEEKABLE (D_TAPE | D_DISK | D_MEM)
fef8985e
MD
243
244/*
245 * Flags for d_flags.
246 */
247#define D_MEMDISK 0x00010000 /* memory type disk */
248#define D_NAGGED 0x00020000 /* nagged about missing make_dev() */
249#define D_CANFREE 0x00040000 /* can free blocks */
250#define D_TRACKCLOSE 0x00080000 /* track all closes */
251#define D_MASTER 0x00100000 /* used by pty/tty code */
d4b8aec4 252#define D_UNUSED200000 0x00200000
9f889dc4 253#define D_MPSAFE 0x00400000 /* all dev_d*() calls are MPSAFE */
fef8985e
MD
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;
fef8985e
MD
265 struct dev_mmap_args du_mmap;
266 struct dev_strategy_args du_strategy;
267 struct dev_dump_args du_dump;
268 struct dev_psize_args du_psize;
269 struct dev_kqfilter_args du_kqfilter;
270 struct dev_clone_args du_clone;
271};
272
273/*
274 * Linking structure for mask/match registration
275 */
276struct dev_ops_link {
277 struct dev_ops_link *next;
278 u_int mask;
279 u_int match;
280 struct dev_ops *ops;
281};
335dda38 282
0e9b9130
MD
283struct dev_ops_maj {
284 RB_ENTRY(dev_ops_maj) rbnode; /* red-black tree of major nums */
285 struct dev_ops_link *link;
286 int maj;
287};
288
289RB_HEAD(dev_ops_rb_tree, dev_ops_maj);
290RB_PROTOTYPE2(dev_ops_rb_tree, dev_ops_maj, rbnode, rb_dev_ops_compare, int);
291
335dda38
MD
292#ifdef _KERNEL
293
fef8985e
MD
294extern struct dev_ops dead_dev_ops;
295
335dda38 296struct disk;
87baaf0c 297struct sysmsg;
335dda38 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,
87baaf0c 304 struct ucred *cred, struct sysmsg *msg);
b24cd69c
AH
305int dev_ddump(cdev_t dev, void *virtual, vm_offset_t physical, off_t offset,
306 size_t length);
e0fc5693 307int64_t dev_dpsize(cdev_t dev);
b13267a5
MD
308int dev_dread(cdev_t dev, struct uio *uio, int ioflag);
309int dev_dwrite(cdev_t dev, struct uio *uio, int ioflag);
b13267a5
MD
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;
fef8985e
MD
328d_mmap_t nommap;
329d_strategy_t nostrategy;
330d_dump_t nodump;
331d_psize_t nopsize;
332d_kqfilter_t nokqfilter;
333d_clone_t noclone;
a32446b7 334d_revoke_t norevoke;
fef8985e
MD
335
336d_open_t nullopen;
337d_close_t nullclose;
338
339extern struct syslink_desc dev_default_desc;
340extern struct syslink_desc dev_open_desc;
341extern struct syslink_desc dev_close_desc;
342extern struct syslink_desc dev_read_desc;
343extern struct syslink_desc dev_write_desc;
344extern struct syslink_desc dev_ioctl_desc;
345extern struct syslink_desc dev_dump_desc;
346extern struct syslink_desc dev_psize_desc;
fef8985e
MD
347extern struct syslink_desc dev_mmap_desc;
348extern struct syslink_desc dev_strategu_desc;
349extern struct syslink_desc dev_kqfilter_desc;
350extern struct syslink_desc dev_clone_desc;
351
352void compile_dev_ops(struct dev_ops *);
cd29885a
MD
353int dev_ops_remove_all(struct dev_ops *ops);
354int dev_ops_remove_minor(struct dev_ops *ops, int minor);
b13267a5
MD
355struct dev_ops *dev_ops_intercept(cdev_t, struct dev_ops *);
356void dev_ops_restore(cdev_t, struct dev_ops *);
fef8985e 357
b13267a5 358cdev_t make_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
fef8985e 359 int perms, const char *fmt, ...) __printflike(6, 7);
8f960aa9
AH
360cdev_t make_dev_covering(struct dev_ops *ops, struct dev_ops *bops, int minor,
361 uid_t uid, gid_t gid, int perms, const char *fmt, ...) __printflike(7, 8);
cd29885a
MD
362cdev_t make_only_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
363 int perms, const char *fmt, ...) __printflike(6, 7);
364cdev_t make_only_devfs_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid,
fcefa6f2 365 int perms, const char *fmt, ...) __printflike(6, 7);
cd29885a 366void destroy_only_dev(cdev_t dev);
fcefa6f2 367int make_dev_alias(cdev_t target, const char *fmt, ...) __printflike(2, 3);
8312ca30 368int destroy_dev_alias(cdev_t target, const char *fmt, ...) __printflike(2, 3);
b96f3782 369cdev_t make_autoclone_dev(struct dev_ops *ops, struct devfs_bitmap *bitmap,
fcefa6f2
SW
370 d_clone_t *nhandler, uid_t uid, gid_t gid, int perms,
371 const char *fmt, ...) __printflike(7, 8);
07dfa375 372void destroy_autoclone_dev(cdev_t dev, struct devfs_bitmap *bitmap);
2a32d680
MD
373void sync_devs(void);
374
335dda38
MD
375#endif
376
377#endif
378