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