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