sysmon_envsysvar.h revision 1.7 1 /* $NetBSD: sysmon_envsysvar.h,v 1.7 2007/07/20 10:40:08 xtraeme Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juan Romero Pardines.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Juan Romero Pardines
21 * for the NetBSD Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifndef _DEV_SYSMON_ENVSYSVAR_H_
40 #define _DEV_SYSMON_ENVSYSVAR_H_
41
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/conf.h>
45 #include <sys/kernel.h>
46 #include <sys/systm.h>
47 #include <sys/mutex.h>
48 #include <sys/workqueue.h>
49
50 #include <dev/sysmon/sysmonvar.h>
51 #include <prop/proplib.h>
52
53 #ifdef ENVSYS_DEBUG
54 #define DPRINTF(x) printf x
55 #else
56 #define DPRINTF(x)
57 #endif
58
59 #ifdef ENVSYS_OBJECTS_DEBUG
60 #define DPRINTFOBJ(x) printf x
61 #else
62 #define DPRINTFOBJ(x)
63 #endif
64
65 /* convenience macros to avoid writing same code many times */
66 #define SENSOR_OBJUPDATED(a, b) \
67 do { \
68 DPRINTFOBJ(("%s: obj (%s:%d) updated\n", __func__, (a), (b))); \
69 } while (/* CONSTCOND */ 0)
70
71 /*
72 * Functions to create objects in a dictionary if they do not exist, or
73 * for updating its value it value provided doesn't match with the value
74 * in dictionary.
75 */
76 static inline int
77 sme_sensor_upbool(prop_object_t obj, prop_dictionary_t dict,
78 const char *key, bool val)
79 {
80 KASSERT(dict != NULL);
81
82 obj = prop_dictionary_get(dict, key);
83 if (obj) {
84 if (prop_bool_true(obj) != val) {
85 if (!prop_dictionary_set_bool(dict, key, val)) {
86 DPRINTF(("%s: (up) set_bool %s:%d\n",
87 __func__, key, val));
88 return EINVAL;
89 }
90 SENSOR_OBJUPDATED(key, val);
91 }
92 } else {
93 if (!prop_dictionary_set_bool(dict, key, val)) {
94 DPRINTF(("%s: (set) set_bool %s:%d\n",
95 __func__, key, val));
96 return EINVAL;
97 }
98 }
99
100 return 0;
101 }
102
103 static inline int
104 sme_sensor_upint32(prop_object_t obj, prop_dictionary_t dict,
105 const char *key, int32_t val)
106 {
107 KASSERT(dict != NULL);
108
109 obj = prop_dictionary_get(dict, key);
110 if (obj) {
111 if (!prop_number_equals_integer(obj, val)) {
112 if (!prop_dictionary_set_int32(dict, key, val)) {
113 DPRINTF(("%s: (up) set_int32 %s:%d\n",
114 __func__, key, val));
115 return EINVAL;
116 }
117 SENSOR_OBJUPDATED(key, val);
118 }
119 } else {
120 if (!prop_dictionary_set_int32(dict, key, val)) {
121 DPRINTF(("%s: (set) set_int32 %s:%d\n",
122 __func__, key, val));
123 return EINVAL;
124 }
125 }
126
127 return 0;
128 }
129
130 static inline int
131 sme_sensor_upuint32(prop_object_t obj, prop_dictionary_t dict,
132 const char *key, uint32_t val)
133 {
134 KASSERT(dict != NULL);
135
136 obj = prop_dictionary_get(dict, key);
137 if (obj) {
138 if (!prop_number_equals_unsigned_integer(obj, val)) {
139 if (!prop_dictionary_set_uint32(dict, key, val)) {
140 DPRINTF(("%s: (up) set_uint32 %s:%d\n",
141 __func__, key, val));
142 return EINVAL;
143 }
144 SENSOR_OBJUPDATED(key, val);
145 }
146 } else {
147 if (!prop_dictionary_set_uint32(dict, key, val)) {
148 DPRINTF(("%s: (set) set_uint32 %s:%d\n",
149 __func__, key, val));
150 return EINVAL;
151 }
152 }
153
154 return 0;
155 }
156
157 static inline int
158 sme_sensor_upstring(prop_object_t obj, prop_dictionary_t dict,
159 const char *key, const char *str)
160 {
161 KASSERT(dict != NULL);
162
163 obj = prop_dictionary_get(dict, key);
164 if (obj == NULL) {
165 if (!prop_dictionary_set_cstring_nocopy(dict, key, str)) {
166 DPRINTF(("%s: (up) set_cstring %s:%s\n",
167 __func__, key, str));
168 return EINVAL;
169 }
170 } else {
171 if (!prop_string_equals_cstring(obj, str)) {
172 if (!prop_dictionary_set_cstring_nocopy(dict,
173 key,
174 str)) {
175 DPRINTF(("%s: (set) set_cstring %s:%s\n",
176 __func__, key, str));
177 return EINVAL;
178 }
179 }
180 }
181
182 return 0;
183 }
184
185 /* struct used by a sysmon envsys event */
186 typedef struct sme_event {
187 /* to add works into our workqueue */
188 struct work see_wk;
189 LIST_ENTRY(sme_event) see_list;
190 struct penvsys_state pes; /* our power envsys */
191 int32_t critval; /* critical value set */
192 int type; /* type of the event */
193 int snum; /* sensor number */
194 int evsent; /* event already sent */
195 int see_flags; /* see above */
196 #define SME_EVENT_WORKING 0x0001 /* This event is busy */
197 } sme_event_t;
198
199 /* struct by a sysmon envsys event set by a driver */
200 typedef struct sme_event_drv {
201 struct sysmon_envsys *sme;
202 prop_dictionary_t sdict;
203 envsys_data_t *edata;
204 int powertype;
205 } sme_event_drv_t;
206
207 /* common */
208 extern kmutex_t sme_mtx; /* mutex for the sysmon envsys devices */
209 extern kmutex_t sme_event_mtx; /* mutex for the sysmon envsys events */
210 extern kmutex_t sme_event_init_mtx; /* mutex to initialize/destroy see */
211 extern kcondvar_t sme_event_cv;
212
213 /* linked list for the sysmon envsys devices */
214 LIST_HEAD(, sysmon_envsys) sysmon_envsys_list;
215
216 /* linked list for the sysmon envsys events */
217 LIST_HEAD(, sme_event) sme_events_list;
218
219 /* functions to handle sysmon envsys devices */
220 int sysmon_envsys_createplist(struct sysmon_envsys *);
221 int sme_make_dictionary(struct sysmon_envsys *, prop_array_t,
222 envsys_data_t *);
223 int sme_update_dictionary(struct sysmon_envsys *);
224 int sme_userset_dictionary(struct sysmon_envsys *,
225 prop_dictionary_t, prop_array_t);
226
227 /* functions to handle sysmon envsys events */
228 int sme_event_register(struct sme_event *);
229 int sme_event_unregister(const char *, int);
230 void sme_event_drvadd(void *);
231 int sme_event_add(prop_dictionary_t, envsys_data_t *,
232 const char *, const char *, int32_t, int, int);
233 int sme_events_init(void);
234 void sme_events_check(void *);
235 void sme_events_worker(struct work *, void *);
236
237 #endif /* _DEV_SYSMON_ENVSYSVAR_H_ */
238