swsensor.c revision 1.2 1 1.2 pooka /* $NetBSD: swsensor.c,v 1.2 2010/10/20 18:50:46 pooka 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.2 pooka __KERNEL_RCSID(0, "$NetBSD: swsensor.c,v 1.2 2010/10/20 18:50:46 pooka 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.1 pgoyette
39 1.1 pgoyette #include <prop/proplib.h>
40 1.1 pgoyette
41 1.1 pgoyette int swsensorattach(int);
42 1.1 pgoyette
43 1.1 pgoyette static struct sysctllog *swsensor_sysctllog = NULL;
44 1.1 pgoyette
45 1.1 pgoyette static int sensor_value_sysctl = 0;
46 1.1 pgoyette
47 1.1 pgoyette static struct sysmon_envsys *swsensor_sme;
48 1.1 pgoyette static envsys_data_t swsensor_edata;
49 1.1 pgoyette
50 1.1 pgoyette static int32_t sw_sensor_value;
51 1.1 pgoyette
52 1.1 pgoyette MODULE(MODULE_CLASS_DRIVER, swsensor, NULL);
53 1.1 pgoyette
54 1.1 pgoyette /*
55 1.1 pgoyette * Set-up the sysctl interface for setting the sensor's cur_value
56 1.1 pgoyette */
57 1.1 pgoyette
58 1.1 pgoyette static
59 1.1 pgoyette void
60 1.1 pgoyette sysctl_swsensor_setup(void)
61 1.1 pgoyette {
62 1.1 pgoyette int ret;
63 1.1 pgoyette int node_sysctl_num;
64 1.1 pgoyette const struct sysctlnode *me = NULL;
65 1.1 pgoyette
66 1.1 pgoyette KASSERT(swsensor_sysctllog == NULL);
67 1.1 pgoyette
68 1.1 pgoyette ret = sysctl_createv(&swsensor_sysctllog, 0, NULL, &me,
69 1.1 pgoyette CTLFLAG_READWRITE,
70 1.1 pgoyette CTLTYPE_NODE, "swsensor", NULL,
71 1.1 pgoyette NULL, 0, NULL, 0,
72 1.1 pgoyette CTL_HW, CTL_CREATE, CTL_EOL);
73 1.1 pgoyette if (ret != 0)
74 1.1 pgoyette return;
75 1.1 pgoyette
76 1.1 pgoyette node_sysctl_num = me->sysctl_num;
77 1.1 pgoyette ret = sysctl_createv(&swsensor_sysctllog, 0, NULL, &me,
78 1.1 pgoyette CTLFLAG_READWRITE,
79 1.1 pgoyette CTLTYPE_INT, "cur_value", NULL,
80 1.1 pgoyette NULL, 0, &sw_sensor_value, 0,
81 1.1 pgoyette CTL_HW, node_sysctl_num, CTL_CREATE, CTL_EOL);
82 1.1 pgoyette
83 1.1 pgoyette if (ret == 0)
84 1.1 pgoyette sensor_value_sysctl = me->sysctl_num;
85 1.1 pgoyette }
86 1.1 pgoyette
87 1.1 pgoyette /*
88 1.1 pgoyette * "Polling" routine to update sensor value
89 1.1 pgoyette */
90 1.1 pgoyette static
91 1.1 pgoyette void
92 1.1 pgoyette swsensor_refresh(struct sysmon_envsys *sme, envsys_data_t *edata)
93 1.1 pgoyette {
94 1.1 pgoyette
95 1.1 pgoyette edata->value_cur = sw_sensor_value;
96 1.1 pgoyette }
97 1.1 pgoyette
98 1.1 pgoyette /*
99 1.1 pgoyette * Module management
100 1.1 pgoyette */
101 1.1 pgoyette
102 1.1 pgoyette static
103 1.1 pgoyette int
104 1.1 pgoyette swsensor_init(void *arg)
105 1.1 pgoyette {
106 1.1 pgoyette int error;
107 1.1 pgoyette prop_dictionary_t pd = (prop_dictionary_t)arg;
108 1.1 pgoyette prop_object_t po = NULL;
109 1.1 pgoyette int pv;
110 1.1 pgoyette
111 1.1 pgoyette swsensor_sme = sysmon_envsys_create();
112 1.1 pgoyette if (swsensor_sme == NULL)
113 1.1 pgoyette return ENOTTY;
114 1.1 pgoyette
115 1.1 pgoyette swsensor_sme->sme_name = "swsensor";
116 1.1 pgoyette swsensor_sme->sme_cookie = &swsensor_edata;
117 1.1 pgoyette swsensor_sme->sme_refresh = swsensor_refresh;
118 1.1 pgoyette swsensor_sme->sme_set_limits = NULL;
119 1.1 pgoyette swsensor_sme->sme_get_limits = NULL;
120 1.1 pgoyette
121 1.1 pgoyette /* See if prop dictionary supplies a sensor type */
122 1.1 pgoyette if (pd != NULL)
123 1.1 pgoyette po = prop_dictionary_get(pd, "type");
124 1.1 pgoyette
125 1.1 pgoyette pv = -1;
126 1.1 pgoyette if (po != NULL && prop_object_type(po) == PROP_TYPE_NUMBER)
127 1.1 pgoyette swsensor_edata.units = prop_number_integer_value(po);
128 1.1 pgoyette else
129 1.1 pgoyette swsensor_edata.units = ENVSYS_INTEGER;
130 1.1 pgoyette
131 1.1 pgoyette /* See if prop dictionary supplies sensor flags */
132 1.1 pgoyette if (pd != NULL)
133 1.1 pgoyette po = prop_dictionary_get(pd, "flags");
134 1.1 pgoyette
135 1.1 pgoyette pv = -1;
136 1.1 pgoyette if (po != NULL && prop_object_type(po) == PROP_TYPE_NUMBER)
137 1.1 pgoyette swsensor_edata.flags = prop_number_integer_value(po);
138 1.1 pgoyette else
139 1.1 pgoyette swsensor_edata.flags = 0;
140 1.1 pgoyette
141 1.1 pgoyette swsensor_edata.value_cur = 0;
142 1.1 pgoyette strlcpy(swsensor_edata.desc, "sensor", ENVSYS_DESCLEN);
143 1.1 pgoyette
144 1.1 pgoyette error = sysmon_envsys_sensor_attach(swsensor_sme, &swsensor_edata);
145 1.1 pgoyette
146 1.1 pgoyette if (error == 0)
147 1.1 pgoyette error = sysmon_envsys_register(swsensor_sme);
148 1.1 pgoyette else {
149 1.1 pgoyette printf("sysmon_envsys_sensor_attach failed: %d\n", error);
150 1.1 pgoyette return error;
151 1.1 pgoyette }
152 1.1 pgoyette
153 1.1 pgoyette if (error == 0)
154 1.1 pgoyette sysctl_swsensor_setup();
155 1.1 pgoyette else
156 1.1 pgoyette printf("sysmon_envsys_register failed: %d\n", error);
157 1.1 pgoyette
158 1.1 pgoyette return error;
159 1.1 pgoyette }
160 1.1 pgoyette
161 1.1 pgoyette static
162 1.1 pgoyette int
163 1.1 pgoyette swsensor_fini(void *arg)
164 1.1 pgoyette {
165 1.1 pgoyette
166 1.1 pgoyette sysmon_envsys_unregister(swsensor_sme);
167 1.1 pgoyette
168 1.1 pgoyette sysctl_teardown(&swsensor_sysctllog);
169 1.1 pgoyette
170 1.1 pgoyette return 0;
171 1.1 pgoyette }
172 1.1 pgoyette
173 1.1 pgoyette static
174 1.1 pgoyette int
175 1.1 pgoyette swsensor_modcmd(modcmd_t cmd, void *arg)
176 1.1 pgoyette {
177 1.1 pgoyette int ret;
178 1.1 pgoyette
179 1.1 pgoyette switch (cmd) {
180 1.1 pgoyette case MODULE_CMD_INIT:
181 1.1 pgoyette ret = swsensor_init(arg);
182 1.1 pgoyette break;
183 1.1 pgoyette
184 1.1 pgoyette case MODULE_CMD_FINI:
185 1.1 pgoyette ret = swsensor_fini(arg);
186 1.1 pgoyette break;
187 1.1 pgoyette
188 1.1 pgoyette case MODULE_CMD_STAT:
189 1.1 pgoyette default:
190 1.1 pgoyette ret = ENOTTY;
191 1.1 pgoyette }
192 1.1 pgoyette
193 1.1 pgoyette return ret;
194 1.1 pgoyette }
195