swsensor.c revision 1.10 1 1.10 pgoyette /* $NetBSD: swsensor.c,v 1.10 2011/06/19 03:12:31 pgoyette Exp $ */
2 1.1 pgoyette /*
3 1.1 pgoyette * Copyright (c) 2008 The NetBSD Foundation, Inc.
4 1.1 pgoyette * All rights reserved.
5 1.1 pgoyette *
6 1.1 pgoyette * Redistribution and use in source and binary forms, with or without
7 1.1 pgoyette * modification, are permitted provided that the following conditions
8 1.1 pgoyette * are met:
9 1.1 pgoyette * 1. Redistributions of source code must retain the above copyright
10 1.1 pgoyette * notice, this list of conditions and the following disclaimer.
11 1.1 pgoyette * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pgoyette * notice, this list of conditions and the following disclaimer in the
13 1.1 pgoyette * documentation and/or other materials provided with the distribution.
14 1.1 pgoyette *
15 1.1 pgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
16 1.1 pgoyette * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17 1.1 pgoyette * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 1.1 pgoyette * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.1 pgoyette * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
20 1.1 pgoyette * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 pgoyette * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
22 1.1 pgoyette * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 pgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
24 1.1 pgoyette * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25 1.1 pgoyette * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
26 1.1 pgoyette * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 1.1 pgoyette */
28 1.1 pgoyette
29 1.1 pgoyette #include <sys/cdefs.h>
30 1.10 pgoyette __KERNEL_RCSID(0, "$NetBSD: swsensor.c,v 1.10 2011/06/19 03:12:31 pgoyette Exp $");
31 1.1 pgoyette
32 1.1 pgoyette #include <sys/param.h>
33 1.1 pgoyette #include <sys/kernel.h>
34 1.1 pgoyette #include <sys/module.h>
35 1.1 pgoyette #include <sys/sysctl.h>
36 1.1 pgoyette
37 1.1 pgoyette #include <dev/sysmon/sysmonvar.h>
38 1.10 pgoyette #include <dev/sysmon/sysmon_envsysvar.h>
39 1.1 pgoyette
40 1.1 pgoyette #include <prop/proplib.h>
41 1.1 pgoyette
42 1.5 pgoyette #ifndef _MODULE
43 1.5 pgoyette #include "opt_modular.h"
44 1.5 pgoyette #endif
45 1.5 pgoyette
46 1.1 pgoyette int swsensorattach(int);
47 1.1 pgoyette
48 1.1 pgoyette static struct sysctllog *swsensor_sysctllog = NULL;
49 1.1 pgoyette
50 1.1 pgoyette static int sensor_value_sysctl = 0;
51 1.1 pgoyette
52 1.1 pgoyette static struct sysmon_envsys *swsensor_sme;
53 1.1 pgoyette static envsys_data_t swsensor_edata;
54 1.1 pgoyette
55 1.1 pgoyette static int32_t sw_sensor_value;
56 1.5 pgoyette static int32_t sw_sensor_limit;
57 1.5 pgoyette static int32_t sw_sensor_mode;
58 1.5 pgoyette static int32_t sw_sensor_defprops;
59 1.5 pgoyette sysmon_envsys_lim_t sw_sensor_deflims;
60 1.1 pgoyette
61 1.1 pgoyette MODULE(MODULE_CLASS_DRIVER, swsensor, NULL);
62 1.1 pgoyette
63 1.1 pgoyette /*
64 1.1 pgoyette * Set-up the sysctl interface for setting the sensor's cur_value
65 1.1 pgoyette */
66 1.1 pgoyette
67 1.1 pgoyette static
68 1.1 pgoyette void
69 1.1 pgoyette sysctl_swsensor_setup(void)
70 1.1 pgoyette {
71 1.1 pgoyette int ret;
72 1.1 pgoyette int node_sysctl_num;
73 1.1 pgoyette const struct sysctlnode *me = NULL;
74 1.1 pgoyette
75 1.1 pgoyette KASSERT(swsensor_sysctllog == NULL);
76 1.1 pgoyette
77 1.1 pgoyette ret = sysctl_createv(&swsensor_sysctllog, 0, NULL, &me,
78 1.1 pgoyette CTLFLAG_READWRITE,
79 1.1 pgoyette CTLTYPE_NODE, "swsensor", NULL,
80 1.1 pgoyette NULL, 0, NULL, 0,
81 1.1 pgoyette CTL_HW, CTL_CREATE, CTL_EOL);
82 1.1 pgoyette if (ret != 0)
83 1.1 pgoyette return;
84 1.1 pgoyette
85 1.1 pgoyette node_sysctl_num = me->sysctl_num;
86 1.1 pgoyette ret = sysctl_createv(&swsensor_sysctllog, 0, NULL, &me,
87 1.1 pgoyette CTLFLAG_READWRITE,
88 1.1 pgoyette CTLTYPE_INT, "cur_value", NULL,
89 1.1 pgoyette NULL, 0, &sw_sensor_value, 0,
90 1.1 pgoyette CTL_HW, node_sysctl_num, CTL_CREATE, CTL_EOL);
91 1.1 pgoyette
92 1.1 pgoyette if (ret == 0)
93 1.1 pgoyette sensor_value_sysctl = me->sysctl_num;
94 1.1 pgoyette }
95 1.1 pgoyette
96 1.1 pgoyette /*
97 1.1 pgoyette * "Polling" routine to update sensor value
98 1.1 pgoyette */
99 1.1 pgoyette static
100 1.1 pgoyette void
101 1.1 pgoyette swsensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
102 1.1 pgoyette {
103 1.1 pgoyette
104 1.1 pgoyette edata->value_cur = sw_sensor_value;
105 1.5 pgoyette
106 1.5 pgoyette /*
107 1.5 pgoyette * Set state. If we're handling the limits ourselves, do the
108 1.5 pgoyette * compare; otherwise just assume the value is valid.
109 1.5 pgoyette */
110 1.5 pgoyette if ((sw_sensor_mode == 2) && (edata->upropset & PROP_CRITMIN) &&
111 1.5 pgoyette (edata->upropset & PROP_DRIVER_LIMITS) &&
112 1.5 pgoyette (edata->value_cur < edata->limits.sel_critmin))
113 1.5 pgoyette edata->state = ENVSYS_SCRITUNDER;
114 1.5 pgoyette else
115 1.5 pgoyette edata->state = ENVSYS_SVALID;
116 1.5 pgoyette }
117 1.5 pgoyette
118 1.5 pgoyette /*
119 1.5 pgoyette * Sensor get/set limit routines
120 1.5 pgoyette */
121 1.5 pgoyette
122 1.5 pgoyette static void
123 1.5 pgoyette swsensor_get_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
124 1.5 pgoyette sysmon_envsys_lim_t *limits, uint32_t *props)
125 1.5 pgoyette {
126 1.5 pgoyette
127 1.5 pgoyette *props = PROP_CRITMIN | PROP_DRIVER_LIMITS;
128 1.5 pgoyette limits->sel_critmin = sw_sensor_limit;
129 1.5 pgoyette }
130 1.5 pgoyette
131 1.5 pgoyette static void
132 1.5 pgoyette swsensor_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
133 1.5 pgoyette sysmon_envsys_lim_t *limits, uint32_t *props)
134 1.5 pgoyette {
135 1.5 pgoyette
136 1.5 pgoyette if (limits == NULL) {
137 1.5 pgoyette limits = &sw_sensor_deflims;
138 1.5 pgoyette props = &sw_sensor_defprops;
139 1.5 pgoyette }
140 1.5 pgoyette if (*props & PROP_CRITMIN)
141 1.5 pgoyette sw_sensor_limit = limits->sel_critmin;
142 1.5 pgoyette
143 1.5 pgoyette /*
144 1.5 pgoyette * If the limit we can handle (crit-min) is set, and no
145 1.5 pgoyette * other limit is set, tell sysmon that the driver will
146 1.5 pgoyette * handle the limit checking.
147 1.5 pgoyette */
148 1.5 pgoyette if ((*props & PROP_LIMITS) == PROP_CRITMIN)
149 1.5 pgoyette *props |= PROP_DRIVER_LIMITS;
150 1.5 pgoyette else
151 1.5 pgoyette *props &= ~PROP_DRIVER_LIMITS;
152 1.1 pgoyette }
153 1.1 pgoyette
154 1.1 pgoyette /*
155 1.1 pgoyette * Module management
156 1.1 pgoyette */
157 1.1 pgoyette
158 1.1 pgoyette static
159 1.1 pgoyette int
160 1.1 pgoyette swsensor_init(void *arg)
161 1.1 pgoyette {
162 1.1 pgoyette int error;
163 1.10 pgoyette const char *key, *str;
164 1.1 pgoyette prop_dictionary_t pd = (prop_dictionary_t)arg;
165 1.10 pgoyette prop_object_t po, obj;
166 1.10 pgoyette prop_object_iterator_t iter;
167 1.10 pgoyette prop_type_t type;
168 1.10 pgoyette const struct sme_descr_entry *descr;
169 1.1 pgoyette
170 1.1 pgoyette swsensor_sme = sysmon_envsys_create();
171 1.1 pgoyette if (swsensor_sme == NULL)
172 1.1 pgoyette return ENOTTY;
173 1.1 pgoyette
174 1.1 pgoyette swsensor_sme->sme_name = "swsensor";
175 1.1 pgoyette swsensor_sme->sme_cookie = &swsensor_edata;
176 1.1 pgoyette swsensor_sme->sme_refresh = swsensor_refresh;
177 1.1 pgoyette swsensor_sme->sme_set_limits = NULL;
178 1.1 pgoyette swsensor_sme->sme_get_limits = NULL;
179 1.1 pgoyette
180 1.10 pgoyette /* Set defaults in case no prop dictionary given */
181 1.1 pgoyette
182 1.10 pgoyette swsensor_edata.units = ENVSYS_INTEGER;
183 1.10 pgoyette swsensor_edata.flags = 0;
184 1.10 pgoyette sw_sensor_mode = 0;
185 1.10 pgoyette sw_sensor_value = 0;
186 1.10 pgoyette sw_sensor_limit = 0;
187 1.1 pgoyette
188 1.10 pgoyette /* Iterate over the provided dictionary, if any */
189 1.10 pgoyette if (pd != NULL) {
190 1.10 pgoyette iter = prop_dictionary_iterator(pd);
191 1.10 pgoyette if (iter == NULL)
192 1.10 pgoyette return ENOMEM;
193 1.10 pgoyette
194 1.10 pgoyette while ((obj = prop_object_iterator_next(iter)) != NULL) {
195 1.10 pgoyette key = prop_dictionary_keysym_cstring_nocopy(obj);
196 1.10 pgoyette po = prop_dictionary_get_keysym(pd, obj);
197 1.10 pgoyette type = prop_object_type(po);
198 1.10 pgoyette
199 1.10 pgoyette /* Sensor type/units */
200 1.10 pgoyette if (strcmp(key, "type") == 0) {
201 1.10 pgoyette if (type == PROP_TYPE_NUMBER) {
202 1.10 pgoyette swsensor_edata.units =
203 1.10 pgoyette prop_number_integer_value(po);
204 1.10 pgoyette continue;
205 1.10 pgoyette }
206 1.10 pgoyette if (type != PROP_TYPE_STRING)
207 1.10 pgoyette return EINVAL;
208 1.10 pgoyette str = prop_dictionary_keysym_cstring_nocopy(po);
209 1.10 pgoyette descr = sme_find_table_desc(SME_DESC_UNITS,
210 1.10 pgoyette str);
211 1.10 pgoyette if (descr->type < 0)
212 1.10 pgoyette return EINVAL;
213 1.10 pgoyette swsensor_edata.units = descr->type;
214 1.10 pgoyette continue;
215 1.10 pgoyette }
216 1.10 pgoyette
217 1.10 pgoyette /* Sensor flags */
218 1.10 pgoyette if (strcmp(key, "flags") == 0) {
219 1.10 pgoyette if (type != PROP_TYPE_NUMBER)
220 1.10 pgoyette return EINVAL;
221 1.10 pgoyette swsensor_edata.flags =
222 1.10 pgoyette prop_number_integer_value(po);
223 1.10 pgoyette continue;
224 1.10 pgoyette }
225 1.10 pgoyette
226 1.10 pgoyette /* Sensor limit behavior
227 1.10 pgoyette * 0 - simple sensor, no hw limits
228 1.10 pgoyette * 1 - simple sensor, hw provides initial limit
229 1.10 pgoyette * 2 - complex sensor, hw provides settable
230 1.10 pgoyette * limits and does its own limit checking
231 1.10 pgoyette */
232 1.10 pgoyette if (strcmp(key, "mode") == 0) {
233 1.10 pgoyette if (type != PROP_TYPE_NUMBER)
234 1.10 pgoyette return EINVAL;
235 1.10 pgoyette sw_sensor_mode = prop_number_integer_value(po);
236 1.10 pgoyette if (sw_sensor_mode > 2)
237 1.10 pgoyette sw_sensor_mode = 2;
238 1.10 pgoyette else if (sw_sensor_mode < 0)
239 1.10 pgoyette sw_sensor_mode = 0;
240 1.10 pgoyette continue;
241 1.10 pgoyette }
242 1.10 pgoyette
243 1.10 pgoyette /* Grab any limit that might be specified */
244 1.10 pgoyette if (strcmp(key, "limit") == 0) {
245 1.10 pgoyette if (type != PROP_TYPE_NUMBER)
246 1.10 pgoyette return EINVAL;
247 1.10 pgoyette sw_sensor_limit = prop_number_integer_value(po);
248 1.10 pgoyette continue;
249 1.10 pgoyette }
250 1.10 pgoyette
251 1.10 pgoyette /* Grab the initial value */
252 1.10 pgoyette if (strcmp(key, "value") == 0) {
253 1.10 pgoyette if (type != PROP_TYPE_NUMBER)
254 1.10 pgoyette return EINVAL;
255 1.10 pgoyette sw_sensor_value = prop_number_integer_value(po);
256 1.10 pgoyette continue;
257 1.10 pgoyette }
258 1.10 pgoyette
259 1.10 pgoyette /* Grab value_min and value_max */
260 1.10 pgoyette if (strcmp(key, "value_min") == 0) {
261 1.10 pgoyette if (type != PROP_TYPE_NUMBER)
262 1.10 pgoyette return EINVAL;
263 1.10 pgoyette swsensor_edata.value_min =
264 1.10 pgoyette prop_number_integer_value(po);
265 1.10 pgoyette swsensor_edata.flags |= ENVSYS_FVALID_MIN;
266 1.10 pgoyette continue;
267 1.10 pgoyette }
268 1.10 pgoyette if (strcmp(key, "value_max") == 0) {
269 1.10 pgoyette if (type != PROP_TYPE_NUMBER)
270 1.10 pgoyette return EINVAL;
271 1.10 pgoyette swsensor_edata.value_max =
272 1.10 pgoyette prop_number_integer_value(po);
273 1.10 pgoyette swsensor_edata.flags |= ENVSYS_FVALID_MAX;
274 1.10 pgoyette continue;
275 1.10 pgoyette }
276 1.10 pgoyette
277 1.10 pgoyette /* See if sensor reports percentages vs raw values */
278 1.10 pgoyette if (strcmp(key, "percentage") == 0) {
279 1.10 pgoyette if (type != PROP_TYPE_BOOL)
280 1.10 pgoyette return EINVAL;
281 1.10 pgoyette if (prop_bool_true(po))
282 1.10 pgoyette swsensor_edata.flags |= ENVSYS_FPERCENT;
283 1.10 pgoyette continue;
284 1.10 pgoyette }
285 1.1 pgoyette
286 1.10 pgoyette /* Unrecognized dicttionary object */
287 1.10 pgoyette return EINVAL;
288 1.1 pgoyette
289 1.10 pgoyette } /* while */
290 1.10 pgoyette prop_object_iterator_release(iter);
291 1.10 pgoyette }
292 1.5 pgoyette
293 1.10 pgoyette /* Initialize limit processing */
294 1.5 pgoyette if (sw_sensor_mode >= 1)
295 1.5 pgoyette swsensor_sme->sme_get_limits = swsensor_get_limits;
296 1.5 pgoyette
297 1.5 pgoyette if (sw_sensor_mode == 2)
298 1.5 pgoyette swsensor_sme->sme_set_limits = swsensor_set_limits;
299 1.5 pgoyette
300 1.5 pgoyette if (sw_sensor_mode != 0) {
301 1.5 pgoyette swsensor_edata.flags |= ENVSYS_FMONLIMITS;
302 1.5 pgoyette swsensor_get_limits(swsensor_sme, &swsensor_edata,
303 1.5 pgoyette &sw_sensor_deflims, &sw_sensor_defprops);
304 1.5 pgoyette }
305 1.5 pgoyette
306 1.10 pgoyette /*
307 1.10 pgoyette * If {min, max} value range was specified, make sure that the
308 1.10 pgoyette * current value is within the range.
309 1.10 pgoyette */
310 1.10 pgoyette if (swsensor_edata.flags & ENVSYS_FVALID_MAX &&
311 1.10 pgoyette sw_sensor_value > swsensor_edata.value_max)
312 1.10 pgoyette swsensor_edata.state = ENVSYS_SINVALID;
313 1.10 pgoyette if (swsensor_edata.flags & ENVSYS_FVALID_MIN &&
314 1.10 pgoyette sw_sensor_value < swsensor_edata.value_min)
315 1.10 pgoyette swsensor_edata.state = ENVSYS_SINVALID;
316 1.10 pgoyette else
317 1.10 pgoyette swsensor_edata.state = ENVSYS_SVALID;
318 1.10 pgoyette swsensor_edata.value_cur = sw_sensor_value;
319 1.5 pgoyette
320 1.1 pgoyette strlcpy(swsensor_edata.desc, "sensor", ENVSYS_DESCLEN);
321 1.1 pgoyette
322 1.1 pgoyette error = sysmon_envsys_sensor_attach(swsensor_sme, &swsensor_edata);
323 1.10 pgoyette if (error != 0) {
324 1.7 pooka aprint_error("sysmon_envsys_sensor_attach failed: %d\n", error);
325 1.1 pgoyette return error;
326 1.1 pgoyette }
327 1.1 pgoyette
328 1.10 pgoyette error = sysmon_envsys_register(swsensor_sme);
329 1.10 pgoyette if (error != 0)
330 1.7 pooka aprint_error("sysmon_envsys_register failed: %d\n", error);
331 1.10 pgoyette return error;
332 1.10 pgoyette
333 1.10 pgoyette sysctl_swsensor_setup();
334 1.10 pgoyette aprint_normal("swsensor: initialized\n");
335 1.1 pgoyette
336 1.10 pgoyette return 0;
337 1.1 pgoyette }
338 1.1 pgoyette
339 1.1 pgoyette static
340 1.1 pgoyette int
341 1.1 pgoyette swsensor_fini(void *arg)
342 1.1 pgoyette {
343 1.1 pgoyette
344 1.1 pgoyette sysmon_envsys_unregister(swsensor_sme);
345 1.1 pgoyette
346 1.1 pgoyette sysctl_teardown(&swsensor_sysctllog);
347 1.1 pgoyette
348 1.1 pgoyette return 0;
349 1.1 pgoyette }
350 1.1 pgoyette
351 1.1 pgoyette static
352 1.1 pgoyette int
353 1.1 pgoyette swsensor_modcmd(modcmd_t cmd, void *arg)
354 1.1 pgoyette {
355 1.1 pgoyette int ret;
356 1.1 pgoyette
357 1.1 pgoyette switch (cmd) {
358 1.1 pgoyette case MODULE_CMD_INIT:
359 1.1 pgoyette ret = swsensor_init(arg);
360 1.1 pgoyette break;
361 1.1 pgoyette
362 1.1 pgoyette case MODULE_CMD_FINI:
363 1.1 pgoyette ret = swsensor_fini(arg);
364 1.1 pgoyette break;
365 1.1 pgoyette
366 1.1 pgoyette case MODULE_CMD_STAT:
367 1.1 pgoyette default:
368 1.1 pgoyette ret = ENOTTY;
369 1.1 pgoyette }
370 1.1 pgoyette
371 1.1 pgoyette return ret;
372 1.1 pgoyette }
373 1.4 pooka
374 1.4 pooka int
375 1.4 pooka swsensorattach(int n __unused)
376 1.4 pooka {
377 1.4 pooka
378 1.5 pgoyette #ifdef MODULAR
379 1.5 pgoyette /*
380 1.5 pgoyette * Modular kernels will automatically load any built-in modules
381 1.5 pgoyette * and call their modcmd() routine, so we don't need to do it
382 1.5 pgoyette * again as part of pseudo-device configuration.
383 1.5 pgoyette */
384 1.5 pgoyette return 0;
385 1.5 pgoyette #else
386 1.4 pooka return swsensor_init(NULL);
387 1.5 pgoyette #endif
388 1.4 pooka }
389