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