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