xref: /openbsd/sys/sys/device.h (revision 5dbfd905)
1 /*	$OpenBSD: device.h,v 1.68 2024/08/04 15:30:08 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 
143 struct cfdriver {
144 	void	**cd_devs;		/* devices found */
145 	char	*cd_name;		/* device name */
146 	enum	devclass cd_class;	/* device classification */
147 	int	cd_mode;		/* device type subclassification */
148 	int	cd_ndevs;		/* size of cd_devs array */
149 };
150 
151 /*
152  * Configuration printing functions, and their return codes.  The second
153  * argument is NULL if the device was configured; otherwise it is the name
154  * of the parent device.  The return value is ignored if the device was
155  * configured, so most functions can return UNCONF unconditionally.
156  */
157 typedef int (*cfprint_t)(void *, const char *);
158 #define	QUIET	0		/* print nothing */
159 #define	UNCONF	1		/* print " not configured\n" */
160 #define	UNSUPP	2		/* print " not supported\n" */
161 
162 /*
163  * Pseudo-device attach information (function + number of pseudo-devs).
164  */
165 struct pdevinit {
166 	void	(*pdev_attach)(int);
167 	int	pdev_count;
168 };
169 
170 #ifdef _KERNEL
171 
172 #ifdef DIAGNOSTIC
173 extern int pdevinit_done;
174 #endif
175 
176 extern struct devicelist alldevs;	/* list of all devices */
177 
178 extern int autoconf_verbose;
179 extern volatile int config_pending;	/* semaphore for mountroot */
180 
181 void config_init(void);
182 void *config_search(cfmatch_t, struct device *, void *);
183 struct device *config_found_sm(struct device *, void *, cfprint_t,
184     cfmatch_t);
185 struct device *config_rootfound(char *, void *);
186 void config_scan(cfscan_t, struct device *);
187 struct device *config_attach(struct device *, void *, void *, cfprint_t);
188 int config_detach(struct device *, int);
189 int config_detach_children(struct device *, int);
190 int config_deactivate(struct device *);
191 int config_suspend(struct device *, int);
192 int config_suspend_all(int);
193 int config_activate_children(struct device *, int);
194 struct device *config_make_softc(struct device *parent,
195     struct cfdata *cf);
196 void config_defer(struct device *, void (*)(struct device *));
197 void config_pending_incr(void);
198 void config_pending_decr(void);
199 void config_mountroot(struct device *, void (*)(struct device *));
200 void config_process_deferred_mountroot(void);
201 
202 int	request_sleep(int);
203 int	sleep_state(void *, int);
204 #define SLEEP_SUSPEND	0x01
205 #define SLEEP_HIBERNATE	0x02
206 void	sleep_mp(void);
207 void	resume_mp(void);
208 int	sleep_showstate(void *v, int sleepmode);
209 int	sleep_setstate(void *v);
210 int	sleep_resume(void *v);
211 int	gosleep(void *v);
212 int	suspend_finish(void *v);
213 
214 struct device *device_mainbus(void);
215 struct device *device_mpath(void);
216 struct device *device_lookup(struct cfdriver *, int unit);
217 void device_ref(struct device *);
218 void device_unref(struct device *);
219 
220 struct nam2blk {
221 	char	*name;
222 	int	maj;
223 };
224 
225 int	findblkmajor(struct device *dv);
226 char	*findblkname(int);
227 void	setroot(struct device *, int, int);
228 struct	device *getdisk(char *str, int len, int defpart, dev_t *devp);
229 struct	device *parsedisk(char *str, int len, int defpart, dev_t *devp);
230 void	device_register(struct device *, void *);
231 void	device_register_wakeup(struct device *);
232 
233 int loadfirmware(const char *name, u_char **bufp, size_t *buflen);
234 #define FIRMWARE_MAX	24*1024*1024
235 
236 /* compatibility definitions */
237 #define config_found(d, a, p)	config_found_sm((d), (a), (p), NULL)
238 
239 #endif /* _KERNEL */
240 
241 #endif /* !_SYS_DEVICE_H_ */
242