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