swsensor.c revision 1.7.8.1 1 1.7.8.1 cherry /* $NetBSD: swsensor.c,v 1.7.8.1 2011/06/23 14:20:09 cherry 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.7.8.1 cherry __KERNEL_RCSID(0, "$NetBSD: swsensor.c,v 1.7.8.1 2011/06/23 14:20:09 cherry 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.7.8.1 cherry #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.7.8.1 cherry /* If value outside of legal range, mark it invalid */
107 1.7.8.1 cherry if ((edata->flags & ENVSYS_FVALID_MIN &&
108 1.7.8.1 cherry edata->value_cur < edata->value_min) ||
109 1.7.8.1 cherry (edata->flags & ENVSYS_FVALID_MAX &&
110 1.7.8.1 cherry edata->value_cur > edata->value_max)) {
111 1.7.8.1 cherry edata->state = ENVSYS_SINVALID;
112 1.7.8.1 cherry return;
113 1.7.8.1 cherry }
114 1.7.8.1 cherry
115 1.5 pgoyette /*
116 1.5 pgoyette * Set state. If we're handling the limits ourselves, do the
117 1.5 pgoyette * compare; otherwise just assume the value is valid.
118 1.5 pgoyette */
119 1.5 pgoyette if ((sw_sensor_mode == 2) && (edata->upropset & PROP_CRITMIN) &&
120 1.5 pgoyette (edata->upropset & PROP_DRIVER_LIMITS) &&
121 1.5 pgoyette (edata->value_cur < edata->limits.sel_critmin))
122 1.5 pgoyette edata->state = ENVSYS_SCRITUNDER;
123 1.5 pgoyette else
124 1.5 pgoyette edata->state = ENVSYS_SVALID;
125 1.5 pgoyette }
126 1.5 pgoyette
127 1.5 pgoyette /*
128 1.5 pgoyette * Sensor get/set limit routines
129 1.5 pgoyette */
130 1.5 pgoyette
131 1.5 pgoyette static void
132 1.5 pgoyette swsensor_get_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 *props = PROP_CRITMIN | PROP_DRIVER_LIMITS;
137 1.5 pgoyette limits->sel_critmin = sw_sensor_limit;
138 1.5 pgoyette }
139 1.5 pgoyette
140 1.5 pgoyette static void
141 1.5 pgoyette swsensor_set_limits(struct sysmon_envsys *sme, envsys_data_t *edata,
142 1.5 pgoyette sysmon_envsys_lim_t *limits, uint32_t *props)
143 1.5 pgoyette {
144 1.5 pgoyette
145 1.5 pgoyette if (limits == NULL) {
146 1.5 pgoyette limits = &sw_sensor_deflims;
147 1.5 pgoyette props = &sw_sensor_defprops;
148 1.5 pgoyette }
149 1.5 pgoyette if (*props & PROP_CRITMIN)
150 1.5 pgoyette sw_sensor_limit = limits->sel_critmin;
151 1.5 pgoyette
152 1.5 pgoyette /*
153 1.5 pgoyette * If the limit we can handle (crit-min) is set, and no
154 1.5 pgoyette * other limit is set, tell sysmon that the driver will
155 1.5 pgoyette * handle the limit checking.
156 1.5 pgoyette */
157 1.5 pgoyette if ((*props & PROP_LIMITS) == PROP_CRITMIN)
158 1.5 pgoyette *props |= PROP_DRIVER_LIMITS;
159 1.5 pgoyette else
160 1.5 pgoyette *props &= ~PROP_DRIVER_LIMITS;
161 1.1 pgoyette }
162 1.1 pgoyette
163 1.1 pgoyette /*
164 1.1 pgoyette * Module management
165 1.1 pgoyette */
166 1.1 pgoyette
167 1.1 pgoyette static
168 1.1 pgoyette int
169 1.1 pgoyette swsensor_init(void *arg)
170 1.1 pgoyette {
171 1.7.8.1 cherry int error, val = 0;
172 1.7.8.1 cherry const char *key, *str;
173 1.1 pgoyette prop_dictionary_t pd = (prop_dictionary_t)arg;
174 1.7.8.1 cherry prop_object_t po, obj;
175 1.7.8.1 cherry prop_object_iterator_t iter;
176 1.7.8.1 cherry prop_type_t type;
177 1.7.8.1 cherry const struct sme_descr_entry *descr;
178 1.1 pgoyette
179 1.1 pgoyette swsensor_sme = sysmon_envsys_create();
180 1.1 pgoyette if (swsensor_sme == NULL)
181 1.1 pgoyette return ENOTTY;
182 1.1 pgoyette
183 1.1 pgoyette swsensor_sme->sme_name = "swsensor";
184 1.1 pgoyette swsensor_sme->sme_cookie = &swsensor_edata;
185 1.1 pgoyette swsensor_sme->sme_refresh = swsensor_refresh;
186 1.1 pgoyette swsensor_sme->sme_set_limits = NULL;
187 1.1 pgoyette swsensor_sme->sme_get_limits = NULL;
188 1.1 pgoyette
189 1.7.8.1 cherry /* Set defaults in case no prop dictionary given */
190 1.1 pgoyette
191 1.7.8.1 cherry swsensor_edata.units = ENVSYS_INTEGER;
192 1.7.8.1 cherry swsensor_edata.flags = 0;
193 1.7.8.1 cherry sw_sensor_mode = 0;
194 1.7.8.1 cherry sw_sensor_value = 0;
195 1.7.8.1 cherry sw_sensor_limit = 0;
196 1.7.8.1 cherry
197 1.7.8.1 cherry /* Iterate over the provided dictionary, if any */
198 1.7.8.1 cherry if (pd != NULL) {
199 1.7.8.1 cherry iter = prop_dictionary_iterator(pd);
200 1.7.8.1 cherry if (iter == NULL)
201 1.7.8.1 cherry return ENOMEM;
202 1.7.8.1 cherry
203 1.7.8.1 cherry while ((obj = prop_object_iterator_next(iter)) != NULL) {
204 1.7.8.1 cherry key = prop_dictionary_keysym_cstring_nocopy(obj);
205 1.7.8.1 cherry po = prop_dictionary_get_keysym(pd, obj);
206 1.7.8.1 cherry type = prop_object_type(po);
207 1.7.8.1 cherry if (type == PROP_TYPE_NUMBER)
208 1.7.8.1 cherry val = prop_number_integer_value(po);
209 1.7.8.1 cherry
210 1.7.8.1 cherry /* Sensor type/units */
211 1.7.8.1 cherry if (strcmp(key, "type") == 0) {
212 1.7.8.1 cherry if (type == PROP_TYPE_NUMBER) {
213 1.7.8.1 cherry descr = sme_find_table_entry(
214 1.7.8.1 cherry SME_DESC_UNITS, val);
215 1.7.8.1 cherry if (descr == NULL)
216 1.7.8.1 cherry return EINVAL;
217 1.7.8.1 cherry swsensor_edata.units = descr->type;
218 1.7.8.1 cherry continue;
219 1.7.8.1 cherry }
220 1.7.8.1 cherry if (type != PROP_TYPE_STRING)
221 1.7.8.1 cherry return EINVAL;
222 1.7.8.1 cherry str = prop_string_cstring_nocopy(po);
223 1.7.8.1 cherry descr = sme_find_table_desc(SME_DESC_UNITS,
224 1.7.8.1 cherry str);
225 1.7.8.1 cherry if (descr == NULL)
226 1.7.8.1 cherry return EINVAL;
227 1.7.8.1 cherry swsensor_edata.units = descr->type;
228 1.7.8.1 cherry continue;
229 1.7.8.1 cherry }
230 1.7.8.1 cherry
231 1.7.8.1 cherry /* Sensor flags */
232 1.7.8.1 cherry if (strcmp(key, "flags") == 0) {
233 1.7.8.1 cherry if (type != PROP_TYPE_NUMBER)
234 1.7.8.1 cherry return EINVAL;
235 1.7.8.1 cherry swsensor_edata.flags = val;
236 1.7.8.1 cherry continue;
237 1.7.8.1 cherry }
238 1.7.8.1 cherry
239 1.7.8.1 cherry /* Sensor limit behavior
240 1.7.8.1 cherry * 0 - simple sensor, no hw limits
241 1.7.8.1 cherry * 1 - simple sensor, hw provides initial limit
242 1.7.8.1 cherry * 2 - complex sensor, hw provides settable
243 1.7.8.1 cherry * limits and does its own limit checking
244 1.7.8.1 cherry */
245 1.7.8.1 cherry if (strcmp(key, "mode") == 0) {
246 1.7.8.1 cherry if (type != PROP_TYPE_NUMBER)
247 1.7.8.1 cherry return EINVAL;
248 1.7.8.1 cherry sw_sensor_mode = val;
249 1.7.8.1 cherry if (sw_sensor_mode > 2)
250 1.7.8.1 cherry sw_sensor_mode = 2;
251 1.7.8.1 cherry else if (sw_sensor_mode < 0)
252 1.7.8.1 cherry sw_sensor_mode = 0;
253 1.7.8.1 cherry continue;
254 1.7.8.1 cherry }
255 1.7.8.1 cherry
256 1.7.8.1 cherry /* Grab any limit that might be specified */
257 1.7.8.1 cherry if (strcmp(key, "limit") == 0) {
258 1.7.8.1 cherry if (type != PROP_TYPE_NUMBER)
259 1.7.8.1 cherry return EINVAL;
260 1.7.8.1 cherry sw_sensor_limit = val;
261 1.7.8.1 cherry continue;
262 1.7.8.1 cherry }
263 1.7.8.1 cherry
264 1.7.8.1 cherry /* Grab the initial value */
265 1.7.8.1 cherry if (strcmp(key, "value") == 0) {
266 1.7.8.1 cherry if (type != PROP_TYPE_NUMBER)
267 1.7.8.1 cherry return EINVAL;
268 1.7.8.1 cherry sw_sensor_value = val;
269 1.7.8.1 cherry continue;
270 1.7.8.1 cherry }
271 1.7.8.1 cherry
272 1.7.8.1 cherry /* Grab value_min and value_max */
273 1.7.8.1 cherry if (strcmp(key, "value_min") == 0) {
274 1.7.8.1 cherry if (type != PROP_TYPE_NUMBER)
275 1.7.8.1 cherry return EINVAL;
276 1.7.8.1 cherry swsensor_edata.value_min = val;
277 1.7.8.1 cherry swsensor_edata.flags |= ENVSYS_FVALID_MIN;
278 1.7.8.1 cherry continue;
279 1.7.8.1 cherry }
280 1.7.8.1 cherry if (strcmp(key, "value_max") == 0) {
281 1.7.8.1 cherry if (type != PROP_TYPE_NUMBER)
282 1.7.8.1 cherry return EINVAL;
283 1.7.8.1 cherry swsensor_edata.value_max = val;
284 1.7.8.1 cherry swsensor_edata.flags |= ENVSYS_FVALID_MAX;
285 1.7.8.1 cherry continue;
286 1.7.8.1 cherry }
287 1.7.8.1 cherry
288 1.7.8.1 cherry /* See if sensor reports percentages vs raw values */
289 1.7.8.1 cherry if (strcmp(key, "percentage") == 0) {
290 1.7.8.1 cherry if (type != PROP_TYPE_BOOL)
291 1.7.8.1 cherry return EINVAL;
292 1.7.8.1 cherry if (prop_bool_true(po))
293 1.7.8.1 cherry swsensor_edata.flags |= ENVSYS_FPERCENT;
294 1.7.8.1 cherry continue;
295 1.7.8.1 cherry }
296 1.7.8.1 cherry
297 1.7.8.1 cherry /* Unrecognized dicttionary object */
298 1.7.8.1 cherry #ifdef DEBUG
299 1.7.8.1 cherry printf("%s: unknown attribute %s\n", __func__, key);
300 1.7.8.1 cherry #endif
301 1.7.8.1 cherry return EINVAL;
302 1.5 pgoyette
303 1.7.8.1 cherry } /* while */
304 1.7.8.1 cherry prop_object_iterator_release(iter);
305 1.7.8.1 cherry }
306 1.5 pgoyette
307 1.7.8.1 cherry /* Initialize limit processing */
308 1.5 pgoyette if (sw_sensor_mode >= 1)
309 1.5 pgoyette swsensor_sme->sme_get_limits = swsensor_get_limits;
310 1.5 pgoyette
311 1.5 pgoyette if (sw_sensor_mode == 2)
312 1.5 pgoyette swsensor_sme->sme_set_limits = swsensor_set_limits;
313 1.5 pgoyette
314 1.5 pgoyette if (sw_sensor_mode != 0) {
315 1.5 pgoyette swsensor_edata.flags |= ENVSYS_FMONLIMITS;
316 1.5 pgoyette swsensor_get_limits(swsensor_sme, &swsensor_edata,
317 1.5 pgoyette &sw_sensor_deflims, &sw_sensor_defprops);
318 1.5 pgoyette }
319 1.5 pgoyette
320 1.1 pgoyette strlcpy(swsensor_edata.desc, "sensor", ENVSYS_DESCLEN);
321 1.1 pgoyette
322 1.7.8.1 cherry /* Wait for refresh to validate the sensor value */
323 1.7.8.1 cherry swsensor_edata.state = ENVSYS_SINVALID;
324 1.1 pgoyette
325 1.7.8.1 cherry error = sysmon_envsys_sensor_attach(swsensor_sme, &swsensor_edata);
326 1.7.8.1 cherry if (error != 0) {
327 1.7 pooka aprint_error("sysmon_envsys_sensor_attach failed: %d\n", error);
328 1.1 pgoyette return error;
329 1.1 pgoyette }
330 1.1 pgoyette
331 1.7.8.1 cherry error = sysmon_envsys_register(swsensor_sme);
332 1.7.8.1 cherry if (error != 0) {
333 1.7 pooka aprint_error("sysmon_envsys_register failed: %d\n", error);
334 1.7.8.1 cherry return error;
335 1.7.8.1 cherry }
336 1.7.8.1 cherry
337 1.7.8.1 cherry sysctl_swsensor_setup();
338 1.7.8.1 cherry aprint_normal("swsensor: initialized\n");
339 1.1 pgoyette
340 1.7.8.1 cherry return 0;
341 1.1 pgoyette }
342 1.1 pgoyette
343 1.1 pgoyette static
344 1.1 pgoyette int
345 1.1 pgoyette swsensor_fini(void *arg)
346 1.1 pgoyette {
347 1.1 pgoyette
348 1.1 pgoyette sysmon_envsys_unregister(swsensor_sme);
349 1.1 pgoyette
350 1.1 pgoyette sysctl_teardown(&swsensor_sysctllog);
351 1.1 pgoyette
352 1.1 pgoyette return 0;
353 1.1 pgoyette }
354 1.1 pgoyette
355 1.1 pgoyette static
356 1.1 pgoyette int
357 1.1 pgoyette swsensor_modcmd(modcmd_t cmd, void *arg)
358 1.1 pgoyette {
359 1.1 pgoyette int ret;
360 1.1 pgoyette
361 1.1 pgoyette switch (cmd) {
362 1.1 pgoyette case MODULE_CMD_INIT:
363 1.1 pgoyette ret = swsensor_init(arg);
364 1.1 pgoyette break;
365 1.1 pgoyette
366 1.1 pgoyette case MODULE_CMD_FINI:
367 1.1 pgoyette ret = swsensor_fini(arg);
368 1.1 pgoyette break;
369 1.1 pgoyette
370 1.1 pgoyette case MODULE_CMD_STAT:
371 1.1 pgoyette default:
372 1.1 pgoyette ret = ENOTTY;
373 1.1 pgoyette }
374 1.1 pgoyette
375 1.1 pgoyette return ret;
376 1.1 pgoyette }
377 1.4 pooka
378 1.4 pooka int
379 1.4 pooka swsensorattach(int n __unused)
380 1.4 pooka {
381 1.4 pooka
382 1.5 pgoyette #ifdef MODULAR
383 1.5 pgoyette /*
384 1.5 pgoyette * Modular kernels will automatically load any built-in modules
385 1.5 pgoyette * and call their modcmd() routine, so we don't need to do it
386 1.5 pgoyette * again as part of pseudo-device configuration.
387 1.5 pgoyette */
388 1.5 pgoyette return 0;
389 1.5 pgoyette #else
390 1.4 pooka return swsensor_init(NULL);
391 1.5 pgoyette #endif
392 1.4 pooka }
393