1/*	$NetBSD$	*/
2
3/*
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
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 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__KERNEL_RCSID(0, "$NetBSD: subr_autoconf.c,v 1.199 2010/01/19 21:54:53 dyoung Exp $");
31
32#include <sys/param.h>
33#include <sys/device.h>
34#include <sys/systm.h>
35
36/*
37 * Accessor functions for the device_t type.
38 */
39devclass_t
40device_class(device_t dev)
41{
42
43	return dev->dv_class;
44}
45
46cfdata_t
47device_cfdata(device_t dev)
48{
49
50	return dev->dv_cfdata;
51}
52
53cfdriver_t
54device_cfdriver(device_t dev)
55{
56
57	return dev->dv_cfdriver;
58}
59
60cfattach_t
61device_cfattach(device_t dev)
62{
63
64	return dev->dv_cfattach;
65}
66
67int
68device_unit(device_t dev)
69{
70
71	return dev->dv_unit;
72}
73
74const char *
75device_xname(device_t dev)
76{
77
78	return dev->dv_xname;
79}
80
81device_t
82device_parent(device_t dev)
83{
84
85	return dev->dv_parent;
86}
87
88bool
89device_activation(device_t dev, devact_level_t level)
90{
91	int active_flags;
92
93	active_flags = DVF_ACTIVE;
94	switch (level) {
95	case DEVACT_LEVEL_FULL:
96		active_flags |= DVF_CLASS_SUSPENDED;
97		/*FALLTHROUGH*/
98	case DEVACT_LEVEL_DRIVER:
99		active_flags |= DVF_DRIVER_SUSPENDED;
100		/*FALLTHROUGH*/
101	case DEVACT_LEVEL_BUS:
102		active_flags |= DVF_BUS_SUSPENDED;
103		break;
104	}
105
106	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
107}
108
109bool
110device_is_active(device_t dev)
111{
112	int active_flags;
113
114	active_flags = DVF_ACTIVE;
115	active_flags |= DVF_CLASS_SUSPENDED;
116	active_flags |= DVF_DRIVER_SUSPENDED;
117	active_flags |= DVF_BUS_SUSPENDED;
118
119	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
120}
121
122bool
123device_is_enabled(device_t dev)
124{
125	return (dev->dv_flags & DVF_ACTIVE) == DVF_ACTIVE;
126}
127
128bool
129device_has_power(device_t dev)
130{
131	int active_flags;
132
133	active_flags = DVF_ACTIVE | DVF_BUS_SUSPENDED;
134
135	return (dev->dv_flags & active_flags) == DVF_ACTIVE;
136}
137
138int
139device_locator(device_t dev, u_int locnum)
140{
141
142	KASSERT(dev->dv_locators != NULL);
143	return dev->dv_locators[locnum];
144}
145
146void *
147device_private(device_t dev)
148{
149
150	/*
151	 * The reason why device_private(NULL) is allowed is to simplify the
152	 * work of a lot of userspace request handlers (i.e., c/bdev
153	 * handlers) which grab cfdriver_t->cd_units[n].
154	 * It avoids having them test for it to be NULL and only then calling
155	 * device_private.
156	 */
157	return dev == NULL ? NULL : dev->dv_private;
158}
159
160prop_dictionary_t
161device_properties(device_t dev)
162{
163
164	return dev->dv_properties;
165}
166
167/*
168 * device_is_a:
169 *
170 *	Returns true if the device is an instance of the specified
171 *	driver.
172 */
173bool
174device_is_a(device_t dev, const char *dname)
175{
176
177	return strcmp(dev->dv_cfdriver->cd_name, dname) == 0;
178}
179