| 1 | /*	$OpenBSD: device.h,v 1.71 2026/03/11 16:18:42 kettenis Exp $	*/ |
| 2 | /*	$NetBSD: device.h,v 1.15 1996/04/09 20:55:24 cgd Exp $	*/ |
| 3 | |
| 4 | /* |
| 5 | * Copyright (c) 1992, 1993 |
| 6 | *	The Regents of the University of California. All rights reserved. |
| 7 | * |
| 8 | * This software was developed by the Computer Systems Engineering group |
| 9 | * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and |
| 10 | * contributed to Berkeley. |
| 11 | * |
| 12 | * All advertising materials mentioning features or use of this software |
| 13 | * must display the following acknowledgement: |
| 14 | *	This product includes software developed by the University of |
| 15 | *	California, Lawrence Berkeley Laboratory. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * 1. Redistributions of source code must retain the above copyright |
| 21 | * notice, this list of conditions and the following disclaimer. |
| 22 | * 2. Redistributions in binary form must reproduce the above copyright |
| 23 | * notice, this list of conditions and the following disclaimer in the |
| 24 | * documentation and/or other materials provided with the distribution. |
| 25 | * 3. Neither the name of the University nor the names of its contributors |
| 26 | * may be used to endorse or promote products derived from this software |
| 27 | * without specific prior written permission. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 30 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 33 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 34 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 35 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 36 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 37 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 38 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 39 | * SUCH DAMAGE. |
| 40 | * |
| 41 | *	@(#)device.h	8.2 (Berkeley) 2/17/94 |
| 42 | */ |
| 43 | |
| 44 | #ifndef _SYS_DEVICE_H_ |
| 45 | #define	_SYS_DEVICE_H_ |
| 46 | |
| 47 | #include <sys/queue.h> |
| 48 | |
| 49 | /* |
| 50 | * Minimal device structures. |
| 51 | * Note that all ``system'' device types are listed here. |
| 52 | */ |
| 53 | enum devclass { |
| 54 | 	DV_DULL,		/* generic, no special info */ |
| 55 | 	DV_CPU,			/* CPU (carries resource utilization) */ |
| 56 | 	DV_DISK,		/* disk drive (label, etc) */ |
| 57 | 	DV_IFNET,		/* network interface */ |
| 58 | 	DV_TAPE,		/* tape device */ |
| 59 | 	DV_TTY			/* serial line interface (???) */ |
| 60 | }; |
| 61 | |
| 62 | /* |
| 63 | * Actions for ca_activate. |
| 64 | */ |
| 65 | #define	DVACT_DEACTIVATE	1	/* deactivate the device */ |
| 66 | #define	DVACT_QUIESCE		2	/* warn the device about suspend */ |
| 67 | #define	DVACT_SUSPEND		3	/* suspend the device */ |
| 68 | #define	DVACT_RESUME		4	/* resume the device */ |
| 69 | #define	DVACT_WAKEUP		5	/* tell device to recover after resume */ |
| 70 | #define	DVACT_POWERDOWN		6	/* power device down */ |
| 71 | |
| 72 | struct device { |
| 73 | 	enum	devclass dv_class;	/* this device's classification */ |
| 74 | 	TAILQ_ENTRY(device) dv_list;	/* entry on list of all devices */ |
| 75 | 	struct	cfdata *dv_cfdata;	/* config data that found us */ |
| 76 | 	int	dv_unit;		/* device unit number */ |
| 77 | 	char	dv_xname[16];		/* external name (name + unit) */ |
| 78 | 	struct	device *dv_parent;	/* pointer to parent device */ |
| 79 | 	int	dv_flags;		/* misc. flags; see below */ |
| 80 | 	int	dv_ref;			/* ref count */ |
| 81 | }; |
| 82 | |
| 83 | /* dv_flags */ |
| 84 | #define	DVF_ACTIVE	0x0001		/* device is activated */ |
| 85 | |
| 86 | TAILQ_HEAD(devicelist, device); |
| 87 | |
| 88 | /* |
| 89 | * Configuration data (i.e., data placed in ioconf.c). |
| 90 | */ |
| 91 | struct cfdata { |
| 92 | 	const struct	cfattach *cf_attach;	/* config attachment */ |
| 93 | 	struct	cfdriver *cf_driver;	/* config driver */ |
| 94 | 	short	cf_unit;		/* unit number */ |
| 95 | 	short	cf_fstate;		/* finding state (below) */ |
| 96 | 	long	*cf_loc;		/* locators (machine dependent) */ |
| 97 | 	int	cf_flags;		/* flags from config */ |
| 98 | 	short	*cf_parents;		/* potential parents */ |
| 99 | 	int	cf_locnames;		/* start of names */ |
| 100 | 	short	cf_starunit1;		/* 1st usable unit number by STAR */ |
| 101 | }; |
| 102 | extern struct cfdata cfdata[]; |
| 103 | #define FSTATE_NOTFOUND	0	/* has not been found */ |
| 104 | #define	FSTATE_FOUND	1	/* has been found */ |
| 105 | #define	FSTATE_STAR	2	/* duplicable */ |
| 106 | #define FSTATE_DNOTFOUND 3	/* has not been found, and is disabled */ |
| 107 | #define FSTATE_DSTAR	4	/* duplicable, and is disabled */ |
| 108 | |
| 109 | typedef int (*cfmatch_t)(struct device *, void *, void *); |
| 110 | typedef void (*cfscan_t)(struct device *, void *); |
| 111 | |
| 112 | /* |
| 113 | * `configuration' attachment and driver (what the machine-independent |
| 114 | * autoconf uses). As devices are found, they are applied against all |
| 115 | * the potential matches. The one with the best match is taken, and a |
| 116 | * device structure (plus any other data desired) is allocated. Pointers |
| 117 | * to these are placed into an array of pointers. The array itself must |
| 118 | * be dynamic since devices can be found long after the machine is up |
| 119 | * and running. |
| 120 | * |
| 121 | * Devices can have multiple configuration attachments if they attach |
| 122 | * to different attributes (busses, or whatever), to allow specification |
| 123 | * of multiple match and attach functions. There is only one configuration |
| 124 | * driver per driver, so that things like unit numbers and the device |
| 125 | * structure array will be shared. |
| 126 | */ |
| 127 | struct cfattach { |
| 128 | 	size_t	 ca_devsize;		/* size of dev data (for malloc) */ |
| 129 | 	cfmatch_t ca_match;		/* returns a match level */ |
| 130 | 	void	(*ca_attach)(struct device *, struct device *, void *); |
| 131 | 	int	(*ca_detach)(struct device *, int); |
| 132 | 	int	(*ca_activate)(struct device *, int); |
| 133 | }; |
| 134 | |
| 135 | /* Flags given to config_detach(), and the ca_detach function. */ |
| 136 | #define	DETACH_FORCE	0x01		/* force detachment; hardware gone */ |
| 137 | #define	DETACH_QUIET	0x02		/* don't print a notice */ |
| 138 | |
| 139 | /* For cd_mode, below */ |
| 140 | #define CD_INDIRECT		1 |
| 141 | #define CD_SKIPHIBERNATE	2 |
| 142 | #define CD_COCOVM		4	/* Allow a device on a VM employing |
| 143 | 					 * confidential computing methods, |
| 144 | 					 * e.g. AMD SEV. */ |
| 145 | |
| 146 | struct cfdriver { |
| 147 | 	void	**cd_devs;		/* devices found */ |
| 148 | 	char	*cd_name;		/* device name */ |
| 149 | 	enum	devclass cd_class;	/* device classification */ |
| 150 | 	int	cd_mode;		/* device type subclassification */ |
| 151 | 	int	cd_ndevs;		/* size of cd_devs array */ |
| 152 | }; |
| 153 | |
| 154 | /* |
| 155 | * Configuration printing functions, and their return codes. The second |
| 156 | * argument is NULL if the device was configured; otherwise it is the name |
| 157 | * of the parent device. The return value is ignored if the device was |
| 158 | * configured, so most functions can return UNCONF unconditionally. |
| 159 | */ |
| 160 | typedef int (*cfprint_t)(void *, const char *); |
| 161 | #define	QUIET	0		/* print nothing */ |
| 162 | #define	UNCONF	1		/* print " not configured\n" */ |
| 163 | #define	UNSUPP	2		/* print " not supported\n" */ |
| 164 | |
| 165 | /* |
| 166 | * Pseudo-device attach information (function + number of pseudo-devs). |
| 167 | */ |
| 168 | struct pdevinit { |
| 169 | 	void	(*pdev_attach)(int); |
| 170 | 	int	pdev_count; |
| 171 | }; |
| 172 | |
| 173 | #ifdef _KERNEL |
| 174 | |
| 175 | #ifdef DIAGNOSTIC |
| 176 | extern int pdevinit_done; |
| 177 | #endif |
| 178 | |
| 179 | extern struct devicelist alldevs;	/* list of all devices */ |
| 180 | |
| 181 | extern int autoconf_verbose; |
| 182 | extern volatile int config_pending;	/* semaphore for mountroot */ |
| 183 | |
| 184 | void config_init(void); |
| 185 | void *config_search(cfmatch_t, struct device *, void *); |
| 186 | struct device *config_found_sm(struct device *, void *, cfprint_t, |
| 187 | cfmatch_t); |
| 188 | struct device *config_rootfound(char *, void *); |
| 189 | void config_scan(cfscan_t, struct device *); |
| 190 | struct device *config_attach(struct device *, void *, void *, cfprint_t); |
| 191 | int config_detach(struct device *, int); |
| 192 | int config_detach_children(struct device *, int); |
| 193 | int config_deactivate(struct device *); |
| 194 | int config_suspend(struct device *, int); |
| 195 | int config_suspend_all(int); |
| 196 | int config_activate_children(struct device *, int); |
| 197 | struct device *config_make_softc(struct device *parent, |
| 198 | struct cfdata *cf); |
| 199 | void config_defer(struct device *, void (*)(struct device *)); |
| 200 | void config_pending_incr(void); |
| 201 | void config_pending_decr(void); |
| 202 | void config_mountroot(struct device *, void (*)(struct device *)); |
| 203 | void config_process_deferred_mountroot(void); |
| 204 | |
| 205 | int	request_sleep(int); |
| 206 | int	sleep_state(void *, int); |
| 207 | #define SLEEP_RESUME	0 |
| 208 | #define SLEEP_SUSPEND	1 |
| 209 | #define SLEEP_HIBERNATE	2 |
| 210 | void	sleep_mp(void); |
| 211 | void	resume_mp(void); |
| 212 | int	sleep_showstate(void *v, int sleepmode); |
| 213 | int	sleep_setstate(void *v); |
| 214 | int	sleep_resume(void *v); |
| 215 | int	gosleep(void *v); |
| 216 | int	suspend_finish(void *v); |
| 217 | int	resuming(void); |
| 218 | |
| 219 | struct device *device_mainbus(void); |
| 220 | struct device *device_mpath(void); |
| 221 | struct device *device_lookup(struct cfdriver *, int unit); |
| 222 | void device_ref(struct device *); |
| 223 | void device_unref(struct device *); |
| 224 | |
| 225 | struct nam2blk { |
| 226 | 	char	*name; |
| 227 | 	int	maj; |
| 228 | }; |
| 229 | |
| 230 | int	findblkmajor(struct device *dv); |
| 231 | char	*findblkname(int); |
| 232 | void	setroot(struct device *, int, int); |
| 233 | struct	device *getdisk(char *str, int len, int defpart, dev_t *devp); |
| 234 | struct	device *parsedisk(char *str, int len, int defpart, dev_t *devp); |
| 235 | void	device_register(struct device *, void *); |
| 236 | void	device_register_wakeup(struct device *); |
| 237 | |
| 238 | int loadfirmware(const char *name, u_char **bufp, size_t *buflen); |
| 239 | #define FIRMWARE_MAX	24*1024*1024 |
| 240 | |
| 241 | /* compatibility definitions */ |
| 242 | #define config_found(d, a, p)	config_found_sm((d), (a), (p), NULL) |
| 243 | |
| 244 | #endif /* _KERNEL */ |
| 245 | |
| 246 | #endif /* !_SYS_DEVICE_H_ */ |