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