1 /* 2 * Copyright (c) 2003,2004 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 * $DragonFly: src/sys/sys/device.h,v 1.6 2006/07/28 02:17:41 dillon Exp $ 35 */ 36 37 #ifndef _SYS_DEVICE_H_ 38 #define _SYS_DEVICE_H_ 39 40 #ifndef _SYS_TYPES_H_ 41 #include <sys/types.h> 42 #endif 43 #ifndef _SYS_SYSLINK_H_ 44 #include <sys/syslink.h> 45 #endif 46 47 /* 48 * This structure is at the base of every device args structure 49 */ 50 struct dev_generic_args { 51 struct syslink_desc *a_desc; 52 dev_t a_dev; 53 }; 54 55 typedef struct dev_generic_args dev_default_args; 56 57 /* 58 * int d_open(dev_t dev, int oflags, int devtype, struct ucred *cred) 59 */ 60 struct dev_open_args { 61 struct dev_generic_args a_head; 62 int a_oflags; 63 int a_devtype; 64 struct ucred *a_cred; 65 }; 66 67 /* 68 * int d_close(dev_t dev, int fflag, int devtype) 69 */ 70 struct dev_close_args { 71 struct dev_generic_args a_head; 72 int a_fflag; 73 int a_devtype; 74 }; 75 76 /* 77 * int d_read(dev_t dev, struct uio *uio, int ioflag) 78 */ 79 struct dev_read_args { 80 struct dev_generic_args a_head; 81 struct uio *a_uio; 82 int a_ioflag; 83 }; 84 85 /* 86 * int d_write(dev_t dev, struct uio *uio, int ioflag) 87 */ 88 struct dev_write_args { 89 struct dev_generic_args a_head; 90 struct uio *a_uio; 91 int a_ioflag; 92 }; 93 94 /* 95 * int d_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, 96 * struct ucred *cred) 97 */ 98 struct dev_ioctl_args { 99 struct dev_generic_args a_head; 100 u_long a_cmd; 101 caddr_t a_data; 102 int a_fflag; 103 struct ucred *a_cred; 104 }; 105 106 /* 107 * int d_poll(dev_t dev, int events) 108 */ 109 struct dev_poll_args { 110 struct dev_generic_args a_head; 111 int a_events; 112 }; 113 114 /* 115 * int d_mmap(dev_t dev, vm_offset_t offset, int nprot) 116 */ 117 struct 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 */ 122 }; 123 124 /* 125 * void d_strategy(dev_t dev, struct bio *bio) 126 */ 127 struct dev_strategy_args { 128 struct dev_generic_args a_head; 129 struct bio *a_bio; 130 }; 131 132 /* 133 * void d_dump(dev_t dev) 134 */ 135 struct dev_dump_args { 136 struct dev_generic_args a_head; 137 u_int a_count; 138 u_int a_blkno; 139 u_int a_secsize; 140 }; 141 142 /* 143 * int d_psize(dev_t dev) 144 */ 145 struct dev_psize_args { 146 struct dev_generic_args a_head; 147 int a_result; 148 }; 149 150 /* 151 * int d_kqfilter(dev_t dev, struct knote *kn) 152 */ 153 struct dev_kqfilter_args { 154 struct dev_generic_args a_head; 155 struct knote *a_kn; 156 int a_result; 157 }; 158 159 struct dev_clone_args { 160 struct dev_generic_args a_head; 161 }; 162 163 /* 164 * Typedefs to help drivers declare the driver routines and such 165 */ 166 typedef int d_default_t (struct dev_generic_args *ap); 167 typedef int d_open_t (struct dev_open_args *ap); 168 typedef int d_close_t (struct dev_close_args *ap); 169 typedef int d_read_t (struct dev_read_args *ap); 170 typedef int d_write_t (struct dev_write_args *ap); 171 typedef int d_ioctl_t (struct dev_ioctl_args *ap); 172 typedef int d_poll_t (struct dev_poll_args *ap); 173 typedef int d_mmap_t (struct dev_mmap_args *ap); 174 typedef int d_strategy_t (struct dev_strategy_args *ap); 175 typedef int d_dump_t (struct dev_dump_args *ap); 176 typedef int d_psize_t (struct dev_psize_args *ap); 177 typedef int d_kqfilter_t (struct dev_kqfilter_args *ap); 178 typedef int d_clone_t (struct dev_clone_args *ap); 179 180 /* 181 * Character device switch table. 182 * 183 * NOTE: positions are hard coded for static structure initialization. 184 */ 185 struct dev_ops { 186 struct { 187 const char *name; /* base name, e.g. 'da' */ 188 int maj; /* major device number */ 189 u_int flags; /* D_XXX flags */ 190 void *data; /* custom driver data */ 191 int refs; /* ref count */ 192 } head; 193 194 #define dev_ops_first_field d_default 195 d_default_t *d_default; 196 d_open_t *d_open; 197 d_close_t *d_close; 198 d_read_t *d_read; 199 d_write_t *d_write; 200 d_ioctl_t *d_ioctl; 201 d_poll_t *d_poll; 202 d_mmap_t *d_mmap; 203 d_strategy_t *d_strategy; 204 d_dump_t *d_dump; 205 d_psize_t *d_psize; 206 d_kqfilter_t *d_kqfilter; 207 d_clone_t *d_clone; /* clone from base dev_ops */ 208 #define dev_ops_last_field d_clone 209 }; 210 211 /* 212 * Types for d_flags. 213 */ 214 #define D_TAPE 0x0001 215 #define D_DISK 0x0002 216 #define D_TTY 0x0004 217 #define D_MEM 0x0008 218 219 #define D_TYPEMASK 0xffff 220 221 /* 222 * Flags for d_flags. 223 */ 224 #define D_MEMDISK 0x00010000 /* memory type disk */ 225 #define D_NAGGED 0x00020000 /* nagged about missing make_dev() */ 226 #define D_CANFREE 0x00040000 /* can free blocks */ 227 #define D_TRACKCLOSE 0x00080000 /* track all closes */ 228 #define D_MASTER 0x00100000 /* used by pty/tty code */ 229 #define D_KQFILTER 0x00200000 /* has kqfilter entry */ 230 231 /* 232 * A union of all possible argument structures. 233 */ 234 union dev_args_union { 235 struct dev_generic_args du_head; 236 struct dev_open_args du_open; 237 struct dev_close_args du_close; 238 struct dev_read_args du_read; 239 struct dev_write_args du_write; 240 struct dev_ioctl_args du_ioctl; 241 struct dev_poll_args du_poll; 242 struct dev_mmap_args du_mmap; 243 struct dev_strategy_args du_strategy; 244 struct dev_dump_args du_dump; 245 struct dev_psize_args du_psize; 246 struct dev_kqfilter_args du_kqfilter; 247 struct dev_clone_args du_clone; 248 }; 249 250 /* 251 * Linking structure for mask/match registration 252 */ 253 struct dev_ops_link { 254 struct dev_ops_link *next; 255 u_int mask; 256 u_int match; 257 struct dev_ops *ops; 258 }; 259 260 #ifdef _KERNEL 261 262 extern struct dev_ops dead_dev_ops; 263 264 struct disk; 265 266 int dev_dopen(dev_t dev, int oflags, int devtype, struct ucred *cred); 267 int dev_dclose(dev_t dev, int fflag, int devtype); 268 void dev_dstrategy(dev_t dev, struct bio *bio); 269 void dev_dstrategy_chain(dev_t dev, struct bio *bio); 270 int dev_dioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, 271 struct ucred *cred); 272 int dev_ddump(dev_t dev); 273 int dev_dpsize(dev_t dev); 274 int dev_dread(dev_t dev, struct uio *uio, int ioflag); 275 int dev_dwrite(dev_t dev, struct uio *uio, int ioflag); 276 int dev_dpoll(dev_t dev, int events); 277 int dev_dkqfilter(dev_t dev, struct knote *kn); 278 int dev_dmmap(dev_t dev, vm_offset_t offset, int nprot); 279 int dev_dclone(dev_t dev); 280 281 const char *dev_dname(dev_t dev); 282 int dev_dmaj(dev_t dev); 283 int dev_dflags(dev_t dev); 284 int dev_doperate(struct dev_generic_args *ap); 285 int dev_doperate_ops(struct dev_ops *, struct dev_generic_args *ap); 286 287 d_default_t nodefault; 288 d_open_t noopen; 289 d_close_t noclose; 290 d_read_t noread; 291 d_write_t nowrite; 292 d_ioctl_t noioctl; 293 d_poll_t nopoll; 294 d_mmap_t nommap; 295 d_strategy_t nostrategy; 296 d_dump_t nodump; 297 d_psize_t nopsize; 298 d_kqfilter_t nokqfilter; 299 d_clone_t noclone; 300 301 d_open_t nullopen; 302 d_close_t nullclose; 303 304 extern struct syslink_desc dev_default_desc; 305 extern struct syslink_desc dev_open_desc; 306 extern struct syslink_desc dev_close_desc; 307 extern struct syslink_desc dev_read_desc; 308 extern struct syslink_desc dev_write_desc; 309 extern struct syslink_desc dev_ioctl_desc; 310 extern struct syslink_desc dev_dump_desc; 311 extern struct syslink_desc dev_psize_desc; 312 extern struct syslink_desc dev_poll_desc; 313 extern struct syslink_desc dev_mmap_desc; 314 extern struct syslink_desc dev_strategu_desc; 315 extern struct syslink_desc dev_kqfilter_desc; 316 extern struct syslink_desc dev_clone_desc; 317 318 void compile_dev_ops(struct dev_ops *); 319 int dev_ops_add(struct dev_ops *, u_int mask, u_int match); 320 int dev_ops_remove(struct dev_ops *, u_int mask, u_int match); 321 void dev_ops_release(struct dev_ops *); 322 struct dev_ops *dev_ops_add_override(dev_t, struct dev_ops *, u_int, u_int); 323 324 struct dev_ops *dev_ops_intercept(dev_t, struct dev_ops *); 325 void dev_ops_restore(dev_t, struct dev_ops *); 326 struct dev_ops *dev_ops_get(int x, int y); 327 328 void destroy_all_devs(struct dev_ops *, u_int mask, u_int match); 329 dev_t make_dev(struct dev_ops *ops, int minor, uid_t uid, gid_t gid, 330 int perms, const char *fmt, ...) __printflike(6, 7); 331 dev_t make_adhoc_dev (struct dev_ops *ops, int minor); 332 333 #endif 334 335 #endif 336 337