xref: /netbsd-src/sys/sys/device_impl.h (revision 6ec5eb416983ac0d8672721e61378a15d1bef3dd)
1*6ec5eb41Sriastradh /*	$NetBSD: device_impl.h,v 1.5 2022/09/13 09:43:33 riastradh Exp $	*/
2b6c8c37fSriastradh 
3b6c8c37fSriastradh /*
4b6c8c37fSriastradh  * Copyright (c) 2022 The NetBSD Foundation, Inc.
5b6c8c37fSriastradh  * All rights reserved.
6b6c8c37fSriastradh  *
7b6c8c37fSriastradh  * Redistribution and use in source and binary forms, with or without
8b6c8c37fSriastradh  * modification, are permitted provided that the following conditions
9b6c8c37fSriastradh  * are met:
10b6c8c37fSriastradh  * 1. Redistributions of source code must retain the above copyright
11b6c8c37fSriastradh  *    notice, this list of conditions and the following disclaimer.
12b6c8c37fSriastradh  * 2. Redistributions in binary form must reproduce the above copyright
13b6c8c37fSriastradh  *    notice, this list of conditions and the following disclaimer in the
14b6c8c37fSriastradh  *    documentation and/or other materials provided with the distribution.
15b6c8c37fSriastradh  *
16b6c8c37fSriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17b6c8c37fSriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18b6c8c37fSriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19b6c8c37fSriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20b6c8c37fSriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21b6c8c37fSriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22b6c8c37fSriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23b6c8c37fSriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24b6c8c37fSriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25b6c8c37fSriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26b6c8c37fSriastradh  * POSSIBILITY OF SUCH DAMAGE.
27b6c8c37fSriastradh  */
28b6c8c37fSriastradh 
29b6c8c37fSriastradh /*
30b6c8c37fSriastradh  * Copyright (c) 1996, 2000 Christopher G. Demetriou
31b6c8c37fSriastradh  * All rights reserved.
32b6c8c37fSriastradh  *
33b6c8c37fSriastradh  * Redistribution and use in source and binary forms, with or without
34b6c8c37fSriastradh  * modification, are permitted provided that the following conditions
35b6c8c37fSriastradh  * are met:
36b6c8c37fSriastradh  * 1. Redistributions of source code must retain the above copyright
37b6c8c37fSriastradh  *    notice, this list of conditions and the following disclaimer.
38b6c8c37fSriastradh  * 2. Redistributions in binary form must reproduce the above copyright
39b6c8c37fSriastradh  *    notice, this list of conditions and the following disclaimer in the
40b6c8c37fSriastradh  *    documentation and/or other materials provided with the distribution.
41b6c8c37fSriastradh  * 3. All advertising materials mentioning features or use of this software
42b6c8c37fSriastradh  *    must display the following acknowledgement:
43b6c8c37fSriastradh  *          This product includes software developed for the
44b6c8c37fSriastradh  *          NetBSD Project.  See http://www.NetBSD.org/ for
45b6c8c37fSriastradh  *          information about NetBSD.
46b6c8c37fSriastradh  * 4. The name of the author may not be used to endorse or promote products
47b6c8c37fSriastradh  *    derived from this software without specific prior written permission.
48b6c8c37fSriastradh  *
49b6c8c37fSriastradh  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50b6c8c37fSriastradh  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51b6c8c37fSriastradh  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52b6c8c37fSriastradh  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53b6c8c37fSriastradh  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54b6c8c37fSriastradh  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55b6c8c37fSriastradh  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56b6c8c37fSriastradh  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57b6c8c37fSriastradh  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58b6c8c37fSriastradh  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59b6c8c37fSriastradh  *
60b6c8c37fSriastradh  * --(license Id: LICENSE.proto,v 1.1 2000/06/13 21:40:26 cgd Exp )--
61b6c8c37fSriastradh  */
62b6c8c37fSriastradh 
63b6c8c37fSriastradh /*
64b6c8c37fSriastradh  * Copyright (c) 1992, 1993
65b6c8c37fSriastradh  *	The Regents of the University of California.  All rights reserved.
66b6c8c37fSriastradh  *
67b6c8c37fSriastradh  * This software was developed by the Computer Systems Engineering group
68b6c8c37fSriastradh  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
69b6c8c37fSriastradh  * contributed to Berkeley.
70b6c8c37fSriastradh  *
71b6c8c37fSriastradh  * All advertising materials mentioning features or use of this software
72b6c8c37fSriastradh  * must display the following acknowledgement:
73b6c8c37fSriastradh  *	This product includes software developed by the University of
74b6c8c37fSriastradh  *	California, Lawrence Berkeley Laboratories.
75b6c8c37fSriastradh  *
76b6c8c37fSriastradh  * Redistribution and use in source and binary forms, with or without
77b6c8c37fSriastradh  * modification, are permitted provided that the following conditions
78b6c8c37fSriastradh  * are met:
79b6c8c37fSriastradh  * 1. Redistributions of source code must retain the above copyright
80b6c8c37fSriastradh  *    notice, this list of conditions and the following disclaimer.
81b6c8c37fSriastradh  * 2. Redistributions in binary form must reproduce the above copyright
82b6c8c37fSriastradh  *    notice, this list of conditions and the following disclaimer in the
83b6c8c37fSriastradh  *    documentation and/or other materials provided with the distribution.
84b6c8c37fSriastradh  * 3. Neither the name of the University nor the names of its contributors
85b6c8c37fSriastradh  *    may be used to endorse or promote products derived from this software
86b6c8c37fSriastradh  *    without specific prior written permission.
87b6c8c37fSriastradh  *
88b6c8c37fSriastradh  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
89b6c8c37fSriastradh  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90b6c8c37fSriastradh  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91b6c8c37fSriastradh  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
92b6c8c37fSriastradh  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
93b6c8c37fSriastradh  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
94b6c8c37fSriastradh  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
95b6c8c37fSriastradh  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
96b6c8c37fSriastradh  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
97b6c8c37fSriastradh  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
98b6c8c37fSriastradh  * SUCH DAMAGE.
99b6c8c37fSriastradh  *
100b6c8c37fSriastradh  *	@(#)device.h	8.2 (Berkeley) 2/17/94
101b6c8c37fSriastradh  */
102b6c8c37fSriastradh 
103b6c8c37fSriastradh #ifndef	_SYS_DEVICE_IMPL_H_
104b6c8c37fSriastradh #define	_SYS_DEVICE_IMPL_H_
105b6c8c37fSriastradh 
106b6c8c37fSriastradh /*
107b6c8c37fSriastradh  * Private autoconf-internal device structures.
108b6c8c37fSriastradh  *
109b6c8c37fSriastradh  * DO NOT USE outside autoconf internals.
110b6c8c37fSriastradh  */
111b6c8c37fSriastradh 
112b6c8c37fSriastradh #include <sys/device.h>
113b6c8c37fSriastradh 
1149b19f100Sriastradh struct device_lock {
1159b19f100Sriastradh 	int		dvl_nwait;
1169b19f100Sriastradh 	int		dvl_nlock;
1179b19f100Sriastradh 	lwp_t		*dvl_holder;
1189b19f100Sriastradh 	kmutex_t	dvl_mtx;
1199b19f100Sriastradh 	kcondvar_t	dvl_cv;
1209b19f100Sriastradh };
1219b19f100Sriastradh 
1229b19f100Sriastradh #define	DEVICE_SUSPENSORS_MAX	16
1239b19f100Sriastradh 
124b6c8c37fSriastradh struct device {
125b6c8c37fSriastradh 	devhandle_t	dv_handle;	/* this device's handle;
126b6c8c37fSriastradh 					   new device_t's get INVALID */
127b6c8c37fSriastradh 	devclass_t	dv_class;	/* this device's classification */
128b6c8c37fSriastradh 	TAILQ_ENTRY(device) dv_list;	/* entry on list of all devices */
129b6c8c37fSriastradh 	cfdata_t	dv_cfdata;	/* config data that found us
130b6c8c37fSriastradh 					   (NULL if pseudo-device) */
131b6c8c37fSriastradh 	cfdriver_t	dv_cfdriver;	/* our cfdriver */
132b6c8c37fSriastradh 	cfattach_t	dv_cfattach;	/* our cfattach */
133b6c8c37fSriastradh 	int		dv_unit;	/* device unit number */
134b6c8c37fSriastradh 					/* external name (name + unit) */
135b6c8c37fSriastradh 	char		dv_xname[DEVICE_XNAME_SIZE];
136b6c8c37fSriastradh 	device_t	dv_parent;	/* pointer to parent device
137b6c8c37fSriastradh 					   (NULL if pseudo- or root node) */
138b6c8c37fSriastradh 	int		dv_depth;	/* number of parents until root */
139b6c8c37fSriastradh 	int		dv_flags;	/* misc. flags; see below */
140b6c8c37fSriastradh 	void		*dv_private;	/* this device's private storage */
141b6c8c37fSriastradh 	int		*dv_locators;	/* our actual locators (optional) */
142b6c8c37fSriastradh 	prop_dictionary_t dv_properties;/* properties dictionary */
143b6c8c37fSriastradh 	struct localcount *dv_localcount;/* reference count */
144b6c8c37fSriastradh 
145b6c8c37fSriastradh 	int		dv_pending;	/* config_pending count */
146b6c8c37fSriastradh 	TAILQ_ENTRY(device) dv_pending_list;
147b6c8c37fSriastradh 
148b6c8c37fSriastradh 	struct lwp	*dv_attaching;	/* thread not yet finished in attach */
149b6c8c37fSriastradh 	struct lwp	*dv_detaching;	/* detach lock (config_misc_lock/cv) */
150*6ec5eb41Sriastradh 	bool		dv_detach_committed;	/* config_misc_lock */
151*6ec5eb41Sriastradh 	bool		dv_detach_done;		/* dv_detaching */
152b6c8c37fSriastradh 
153b6c8c37fSriastradh 	size_t		dv_activity_count;
154b6c8c37fSriastradh 	void		(**dv_activity_handlers)(device_t, devactive_t);
155b6c8c37fSriastradh 
156b6c8c37fSriastradh 	bool		(*dv_driver_suspend)(device_t, const pmf_qual_t *);
157b6c8c37fSriastradh 	bool		(*dv_driver_resume)(device_t, const pmf_qual_t *);
158b6c8c37fSriastradh 	bool		(*dv_driver_shutdown)(device_t, int);
15931786995Sriastradh 	void		(*dv_driver_child_register)(device_t);
160b6c8c37fSriastradh 
161b6c8c37fSriastradh 	void		*dv_bus_private;
162b6c8c37fSriastradh 	bool		(*dv_bus_suspend)(device_t, const pmf_qual_t *);
163b6c8c37fSriastradh 	bool		(*dv_bus_resume)(device_t, const pmf_qual_t *);
164b6c8c37fSriastradh 	bool		(*dv_bus_shutdown)(device_t, int);
165b6c8c37fSriastradh 	void		(*dv_bus_deregister)(device_t);
166b6c8c37fSriastradh 
167b6c8c37fSriastradh 	void		*dv_class_private;
168b6c8c37fSriastradh 	bool		(*dv_class_suspend)(device_t, const pmf_qual_t *);
169b6c8c37fSriastradh 	bool		(*dv_class_resume)(device_t, const pmf_qual_t *);
170b6c8c37fSriastradh 	void		(*dv_class_deregister)(device_t);
171b6c8c37fSriastradh 
172b6c8c37fSriastradh 	devgen_t		dv_add_gen,
173b6c8c37fSriastradh 				dv_del_gen;
174b6c8c37fSriastradh 
175b6c8c37fSriastradh 	struct device_lock	dv_lock;
176b6c8c37fSriastradh 	const device_suspensor_t
177b6c8c37fSriastradh 	    *dv_bus_suspensors[DEVICE_SUSPENSORS_MAX],
178b6c8c37fSriastradh 	    *dv_driver_suspensors[DEVICE_SUSPENSORS_MAX],
179b6c8c37fSriastradh 	    *dv_class_suspensors[DEVICE_SUSPENSORS_MAX];
180b6c8c37fSriastradh 	struct device_garbage dv_garbage;
181b6c8c37fSriastradh };
182b6c8c37fSriastradh 
183b6c8c37fSriastradh /*
184b6c8c37fSriastradh  * struct device::dv_flags (must not overlap with device.h struct
185b6c8c37fSriastradh  * cfattach::ca_flags for now)
186b6c8c37fSriastradh  */
187b6c8c37fSriastradh #define	DVF_ACTIVE		0x0001	/* device is activated */
188b6c8c37fSriastradh #define	DVF_POWER_HANDLERS	0x0004	/* device has suspend/resume support */
189b6c8c37fSriastradh #define	DVF_CLASS_SUSPENDED	0x0008	/* device class suspend was called */
190b6c8c37fSriastradh #define	DVF_DRIVER_SUSPENDED	0x0010	/* device driver suspend was called */
191b6c8c37fSriastradh #define	DVF_BUS_SUSPENDED	0x0020	/* device bus suspend was called */
192b6c8c37fSriastradh #define	DVF_ATTACH_INPROGRESS	0x0040	/* device attach is in progress */
193b6c8c37fSriastradh 
1949b19f100Sriastradh bool		device_pmf_is_registered(device_t);
1959b19f100Sriastradh 
1969b19f100Sriastradh bool		device_pmf_driver_suspend(device_t, const pmf_qual_t *);
1979b19f100Sriastradh bool		device_pmf_driver_resume(device_t, const pmf_qual_t *);
1989b19f100Sriastradh bool		device_pmf_driver_shutdown(device_t, int);
1999b19f100Sriastradh 
2009b19f100Sriastradh void		device_pmf_driver_register(device_t,
2019b19f100Sriastradh 		    bool (*)(device_t, const pmf_qual_t *),
2029b19f100Sriastradh 		    bool (*)(device_t, const pmf_qual_t *),
2039b19f100Sriastradh 		    bool (*)(device_t, int));
2049b19f100Sriastradh void		device_pmf_driver_deregister(device_t);
2059b19f100Sriastradh 
2069b19f100Sriastradh device_lock_t	device_getlock(device_t);
2079b19f100Sriastradh void		device_pmf_unlock(device_t);
2089b19f100Sriastradh bool		device_pmf_lock(device_t);
2099b19f100Sriastradh 
2109b19f100Sriastradh void		*device_pmf_class_private(device_t);
2119b19f100Sriastradh bool		device_pmf_class_suspend(device_t, const pmf_qual_t *);
2129b19f100Sriastradh bool		device_pmf_class_resume(device_t, const pmf_qual_t *);
2139b19f100Sriastradh 
2149b19f100Sriastradh void		device_pmf_class_register(device_t, void *,
2159b19f100Sriastradh 		    bool (*)(device_t, const pmf_qual_t *),
2169b19f100Sriastradh 		    bool (*)(device_t, const pmf_qual_t *),
2179b19f100Sriastradh 		    void (*)(device_t));
2189b19f100Sriastradh void		device_pmf_class_deregister(device_t);
2199b19f100Sriastradh 
220b6c8c37fSriastradh #endif	/* _SYS_DEVICE_IMPL_H_ */
221