apple_smc_fan.c revision 1.4.4.2 1 1.4.4.2 rmind /* $NetBSD: apple_smc_fan.c,v 1.4.4.2 2014/05/18 17:45:37 rmind Exp $ */
2 1.4.4.2 rmind
3 1.4.4.2 rmind /*
4 1.4.4.2 rmind * Apple System Management Controller: Fans
5 1.4.4.2 rmind */
6 1.4.4.2 rmind
7 1.4.4.2 rmind /*-
8 1.4.4.2 rmind * Copyright (c) 2013 The NetBSD Foundation, Inc.
9 1.4.4.2 rmind * All rights reserved.
10 1.4.4.2 rmind *
11 1.4.4.2 rmind * This code is derived from software contributed to The NetBSD Foundation
12 1.4.4.2 rmind * by Taylor R. Campbell.
13 1.4.4.2 rmind *
14 1.4.4.2 rmind * Redistribution and use in source and binary forms, with or without
15 1.4.4.2 rmind * modification, are permitted provided that the following conditions
16 1.4.4.2 rmind * are met:
17 1.4.4.2 rmind * 1. Redistributions of source code must retain the above copyright
18 1.4.4.2 rmind * notice, this list of conditions and the following disclaimer.
19 1.4.4.2 rmind * 2. Redistributions in binary form must reproduce the above copyright
20 1.4.4.2 rmind * notice, this list of conditions and the following disclaimer in the
21 1.4.4.2 rmind * documentation and/or other materials provided with the distribution.
22 1.4.4.2 rmind *
23 1.4.4.2 rmind * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24 1.4.4.2 rmind * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 1.4.4.2 rmind * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26 1.4.4.2 rmind * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27 1.4.4.2 rmind * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 1.4.4.2 rmind * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29 1.4.4.2 rmind * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30 1.4.4.2 rmind * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31 1.4.4.2 rmind * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32 1.4.4.2 rmind * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 1.4.4.2 rmind * POSSIBILITY OF SUCH DAMAGE.
34 1.4.4.2 rmind */
35 1.4.4.2 rmind
36 1.4.4.2 rmind #include <sys/cdefs.h>
37 1.4.4.2 rmind __KERNEL_RCSID(0, "$NetBSD: apple_smc_fan.c,v 1.4.4.2 2014/05/18 17:45:37 rmind Exp $");
38 1.4.4.2 rmind
39 1.4.4.2 rmind #include <sys/types.h>
40 1.4.4.2 rmind #include <sys/param.h>
41 1.4.4.2 rmind #include <sys/device.h>
42 1.4.4.2 rmind #include <sys/kmem.h>
43 1.4.4.2 rmind #include <sys/module.h>
44 1.4.4.2 rmind #if 0 /* XXX sysctl */
45 1.4.4.2 rmind #include <sys/sysctl.h>
46 1.4.4.2 rmind #endif
47 1.4.4.2 rmind #include <sys/systm.h>
48 1.4.4.2 rmind
49 1.4.4.2 rmind #include <dev/ic/apple_smc.h>
50 1.4.4.2 rmind
51 1.4.4.2 rmind #include <dev/sysmon/sysmonvar.h>
52 1.4.4.2 rmind
53 1.4.4.2 rmind #define APPLE_SMC_NFANS_KEY "FNum"
54 1.4.4.2 rmind
55 1.4.4.2 rmind static const struct fan_sensor {
56 1.4.4.2 rmind const char *fs_name;
57 1.4.4.2 rmind const char *fs_key_suffix;
58 1.4.4.2 rmind } fan_sensors[] = {
59 1.4.4.2 rmind { "actual", "Ac" },
60 1.4.4.2 rmind { "minimum", "Mn" },
61 1.4.4.2 rmind { "maximum", "Mx" },
62 1.4.4.2 rmind { "safe", "Sf" },
63 1.4.4.2 rmind { "target", "Tg" },
64 1.4.4.2 rmind };
65 1.4.4.2 rmind
66 1.4.4.2 rmind struct apple_smc_fan_softc {
67 1.4.4.2 rmind device_t sc_dev;
68 1.4.4.2 rmind struct apple_smc_tag *sc_smc;
69 1.4.4.2 rmind struct sysmon_envsys *sc_sme;
70 1.4.4.2 rmind uint8_t sc_nfans;
71 1.4.4.2 rmind struct {
72 1.4.4.2 rmind struct {
73 1.4.4.2 rmind struct apple_smc_key *sensor_key;
74 1.4.4.2 rmind struct envsys_data sensor_data;
75 1.4.4.2 rmind } sensors[__arraycount(fan_sensors)];
76 1.4.4.2 rmind } *sc_fans;
77 1.4.4.2 rmind
78 1.4.4.2 rmind #if 0 /* XXX sysctl */
79 1.4.4.2 rmind struct sysctllog *sc_sysctl_log;
80 1.4.4.2 rmind const struct sysctlnode *sc_sysctl_node;
81 1.4.4.2 rmind #endif
82 1.4.4.2 rmind };
83 1.4.4.2 rmind
84 1.4.4.2 rmind struct fan_desc {
85 1.4.4.2 rmind uint8_t fd_type;
86 1.4.4.2 rmind uint8_t fd_zone;
87 1.4.4.2 rmind uint8_t fd_location;
88 1.4.4.2 rmind uint8_t fd_reserved0;
89 1.4.4.2 rmind char fd_name[12];
90 1.4.4.2 rmind } __packed;
91 1.4.4.2 rmind
92 1.4.4.2 rmind static int apple_smc_fan_match(device_t, cfdata_t, void *);
93 1.4.4.2 rmind static void apple_smc_fan_attach(device_t, device_t, void *);
94 1.4.4.2 rmind static int apple_smc_fan_detach(device_t, int);
95 1.4.4.2 rmind static int apple_smc_fan_attach_sensors(struct apple_smc_fan_softc *);
96 1.4.4.2 rmind static void apple_smc_fan_attach_sensor(struct apple_smc_fan_softc *,
97 1.4.4.2 rmind uint8_t, const char *, uint8_t);
98 1.4.4.2 rmind static void apple_smc_fan_refresh(struct sysmon_envsys *,
99 1.4.4.2 rmind struct envsys_data *);
100 1.4.4.2 rmind static void apple_smc_fan_release_keys(struct apple_smc_fan_softc *);
101 1.4.4.2 rmind #if 0 /* XXX sysctl */
102 1.4.4.2 rmind static int apple_smc_fan_sysctl_setup(struct apple_smc_fan_softc *);
103 1.4.4.2 rmind static void apple_smc_fan_sysctl_setup_1(struct apple_smc_tag *,
104 1.4.4.2 rmind uint8_t);
105 1.4.4.2 rmind #endif
106 1.4.4.2 rmind
107 1.4.4.2 rmind CFATTACH_DECL_NEW(apple_smc_fan, sizeof(struct apple_smc_fan_softc),
108 1.4.4.2 rmind apple_smc_fan_match, apple_smc_fan_attach, apple_smc_fan_detach, NULL);
109 1.4.4.2 rmind
110 1.4.4.2 rmind static int
111 1.4.4.2 rmind apple_smc_fan_match(device_t parent, cfdata_t match, void *aux)
112 1.4.4.2 rmind {
113 1.4.4.2 rmind const struct apple_smc_attach_args *asa = aux;
114 1.4.4.2 rmind struct apple_smc_key *nfans_key;
115 1.4.4.2 rmind uint8_t nfans;
116 1.4.4.2 rmind int rv = 0;
117 1.4.4.2 rmind int error;
118 1.4.4.2 rmind
119 1.4.4.2 rmind /* Find how to find how many fans there are. */
120 1.4.4.2 rmind error = apple_smc_named_key(asa->asa_smc, APPLE_SMC_NFANS_KEY,
121 1.4.4.2 rmind APPLE_SMC_TYPE_UINT8, &nfans_key);
122 1.4.4.2 rmind if (error)
123 1.4.4.2 rmind goto out0;
124 1.4.4.2 rmind
125 1.4.4.2 rmind /* Find how many fans there are. */
126 1.4.4.2 rmind error = apple_smc_read_key_1(asa->asa_smc, nfans_key, &nfans);
127 1.4.4.2 rmind if (error)
128 1.4.4.2 rmind goto out1;
129 1.4.4.2 rmind
130 1.4.4.2 rmind /* Attach only if there's at least one fan. */
131 1.4.4.2 rmind if (nfans > 0)
132 1.4.4.2 rmind rv = 1;
133 1.4.4.2 rmind
134 1.4.4.2 rmind out1: apple_smc_release_key(asa->asa_smc, nfans_key);
135 1.4.4.2 rmind out0: return rv;
136 1.4.4.2 rmind }
137 1.4.4.2 rmind
138 1.4.4.2 rmind static void
139 1.4.4.2 rmind apple_smc_fan_attach(device_t parent, device_t self, void *aux)
140 1.4.4.2 rmind {
141 1.4.4.2 rmind struct apple_smc_fan_softc *sc = device_private(self);
142 1.4.4.2 rmind const struct apple_smc_attach_args *asa = aux;
143 1.4.4.2 rmind struct apple_smc_key *nfans_key;
144 1.4.4.2 rmind int error;
145 1.4.4.2 rmind
146 1.4.4.2 rmind /* Identify ourselves. */
147 1.4.4.2 rmind aprint_normal(": Apple SMC fan sensors\n");
148 1.4.4.2 rmind
149 1.4.4.2 rmind /* Initialize the softc. */
150 1.4.4.2 rmind sc->sc_dev = self;
151 1.4.4.2 rmind sc->sc_smc = asa->asa_smc;
152 1.4.4.2 rmind
153 1.4.4.2 rmind /* Find how to find how many fans there are. */
154 1.4.4.2 rmind error = apple_smc_named_key(sc->sc_smc, APPLE_SMC_NFANS_KEY,
155 1.4.4.2 rmind APPLE_SMC_TYPE_UINT8, &nfans_key);
156 1.4.4.2 rmind if (error)
157 1.4.4.2 rmind goto out0;
158 1.4.4.2 rmind
159 1.4.4.2 rmind /* Find how many fans there are. */
160 1.4.4.2 rmind error = apple_smc_read_key_1(sc->sc_smc, nfans_key, &sc->sc_nfans);
161 1.4.4.2 rmind if (error)
162 1.4.4.2 rmind goto out1;
163 1.4.4.2 rmind
164 1.4.4.2 rmind /*
165 1.4.4.2 rmind * There should be at least one, but just in case the hardware
166 1.4.4.2 rmind * changed its mind in the interim...
167 1.4.4.2 rmind */
168 1.4.4.2 rmind if (sc->sc_nfans == 0) {
169 1.4.4.2 rmind aprint_error_dev(self, "no fans\n");
170 1.4.4.2 rmind goto out1;
171 1.4.4.2 rmind }
172 1.4.4.2 rmind
173 1.4.4.2 rmind /*
174 1.4.4.2 rmind * The number of fans must fit in a single decimal digit for
175 1.4.4.2 rmind * the names of the fan keys; see the fan_sensor table above.
176 1.4.4.2 rmind */
177 1.4.4.2 rmind if (sc->sc_nfans >= 10) {
178 1.4.4.2 rmind aprint_error_dev(self, "too many fans: %"PRIu8"\n",
179 1.4.4.2 rmind sc->sc_nfans);
180 1.4.4.2 rmind sc->sc_nfans = 9;
181 1.4.4.2 rmind }
182 1.4.4.2 rmind
183 1.4.4.2 rmind #if 0 /* XXX sysctl */
184 1.4.4.2 rmind /* Set up the sysctl tree for controlling the fans. */
185 1.4.4.2 rmind error = apple_smc_fan_sysctl_setup(sc);
186 1.4.4.2 rmind if (error)
187 1.4.4.2 rmind goto fail0;
188 1.4.4.2 rmind #endif
189 1.4.4.2 rmind
190 1.4.4.2 rmind /* Attach the sensors to sysmon_envsys. */
191 1.4.4.2 rmind error = apple_smc_fan_attach_sensors(sc);
192 1.4.4.2 rmind if (error)
193 1.4.4.2 rmind goto fail1;
194 1.4.4.2 rmind
195 1.4.4.2 rmind /* Success! */
196 1.4.4.2 rmind goto out1;
197 1.4.4.2 rmind
198 1.4.4.2 rmind #if 0
199 1.4.4.2 rmind fail2:
200 1.4.4.2 rmind apple_smc_fan_detach_sensors(sc);
201 1.4.4.2 rmind #endif
202 1.4.4.2 rmind
203 1.4.4.2 rmind fail1:
204 1.4.4.2 rmind #if 0 /* XXX sysctl */
205 1.4.4.2 rmind sysctl_teardown(&sc->sc_sysctl_log);
206 1.4.4.2 rmind fail0:
207 1.4.4.2 rmind #endif
208 1.4.4.2 rmind
209 1.4.4.2 rmind out1: apple_smc_release_key(sc->sc_smc, nfans_key);
210 1.4.4.2 rmind out0: return;
211 1.4.4.2 rmind }
212 1.4.4.2 rmind
213 1.4.4.2 rmind static int
214 1.4.4.2 rmind apple_smc_fan_detach(device_t self, int flags)
215 1.4.4.2 rmind {
216 1.4.4.2 rmind struct apple_smc_fan_softc *sc = device_private(self);
217 1.4.4.2 rmind
218 1.4.4.2 rmind /* If we registered with sysmon_envsys, unregister. */
219 1.4.4.2 rmind if (sc->sc_sme != NULL) {
220 1.4.4.2 rmind sysmon_envsys_unregister(sc->sc_sme);
221 1.4.4.2 rmind sc->sc_sme = NULL;
222 1.4.4.2 rmind
223 1.4.4.2 rmind KASSERT(sc->sc_fans != NULL);
224 1.4.4.2 rmind KASSERT(sc->sc_nfans > 0);
225 1.4.4.2 rmind KASSERT(sc->sc_nfans < 10);
226 1.4.4.2 rmind
227 1.4.4.2 rmind /* Release the keys and free the memory for fan records. */
228 1.4.4.2 rmind apple_smc_fan_release_keys(sc);
229 1.4.4.2 rmind kmem_free(sc->sc_fans,
230 1.4.4.2 rmind (sizeof(sc->sc_fans[0]) * sc->sc_nfans));
231 1.4.4.2 rmind sc->sc_fans = NULL;
232 1.4.4.2 rmind sc->sc_nfans = 0;
233 1.4.4.2 rmind }
234 1.4.4.2 rmind
235 1.4.4.2 rmind #if 0 /* XXX sysctl */
236 1.4.4.2 rmind /* Tear down all the sysctl knobs we set up. */
237 1.4.4.2 rmind sysctl_teardown(&sc->sc_sysctl_log);
238 1.4.4.2 rmind #endif
239 1.4.4.2 rmind
240 1.4.4.2 rmind return 0;
241 1.4.4.2 rmind }
242 1.4.4.2 rmind
243 1.4.4.2 rmind static int
244 1.4.4.2 rmind apple_smc_fan_attach_sensors(struct apple_smc_fan_softc *sc)
245 1.4.4.2 rmind {
246 1.4.4.2 rmind uint8_t fan, sensor;
247 1.4.4.2 rmind char fan_desc_key_name[4 + 1];
248 1.4.4.2 rmind struct apple_smc_key *fan_desc_key;
249 1.4.4.2 rmind struct fan_desc fan_desc;
250 1.4.4.2 rmind char name[sizeof(fan_desc.fd_name) + 1];
251 1.4.4.2 rmind int error;
252 1.4.4.2 rmind
253 1.4.4.2 rmind /* Create a sysmon_envsys record, but don't register it yet. */
254 1.4.4.2 rmind sc->sc_sme = sysmon_envsys_create();
255 1.4.4.2 rmind sc->sc_sme->sme_name = device_xname(sc->sc_dev);
256 1.4.4.2 rmind sc->sc_sme->sme_cookie = sc;
257 1.4.4.2 rmind sc->sc_sme->sme_refresh = apple_smc_fan_refresh;
258 1.4.4.2 rmind
259 1.4.4.2 rmind /* Create an array of fan sensor records. */
260 1.4.4.2 rmind CTASSERT(10 <= (SIZE_MAX / sizeof(sc->sc_fans[0])));
261 1.4.4.2 rmind sc->sc_fans = kmem_zalloc((sizeof(sc->sc_fans[0]) * sc->sc_nfans),
262 1.4.4.2 rmind KM_SLEEP);
263 1.4.4.2 rmind
264 1.4.4.2 rmind /* Find all the fans. */
265 1.4.4.2 rmind for (fan = 0; fan < sc->sc_nfans; fan++) {
266 1.4.4.2 rmind
267 1.4.4.2 rmind /* Format the name of the key for the fan's description. */
268 1.4.4.2 rmind (void)snprintf(fan_desc_key_name, sizeof(fan_desc_key_name),
269 1.4.4.2 rmind "F%"PRIu8"ID", fan);
270 1.4.4.2 rmind KASSERT(4 == strlen(fan_desc_key_name));
271 1.4.4.2 rmind
272 1.4.4.2 rmind /* Look up the key for this fan's description. */
273 1.4.4.2 rmind error = apple_smc_named_key(sc->sc_smc, fan_desc_key_name,
274 1.4.4.2 rmind APPLE_SMC_TYPE_FANDESC, &fan_desc_key);
275 1.4.4.2 rmind if (error) {
276 1.4.4.2 rmind aprint_error_dev(sc->sc_dev,
277 1.4.4.2 rmind "error identifying fan %"PRIu8": %d\n",
278 1.4.4.2 rmind fan, error);
279 1.4.4.2 rmind continue;
280 1.4.4.2 rmind }
281 1.4.4.2 rmind
282 1.4.4.2 rmind /* Read the description of this fan. */
283 1.4.4.2 rmind error = apple_smc_read_key(sc->sc_smc, fan_desc_key, &fan_desc,
284 1.4.4.2 rmind sizeof(fan_desc));
285 1.4.4.2 rmind if (error) {
286 1.4.4.2 rmind aprint_error_dev(sc->sc_dev,
287 1.4.4.2 rmind "error identifying fan %"PRIu8": %d\n",
288 1.4.4.2 rmind fan, error);
289 1.4.4.2 rmind continue;
290 1.4.4.2 rmind }
291 1.4.4.2 rmind
292 1.4.4.2 rmind /*
293 1.4.4.2 rmind * XXX Do more with the fan description...
294 1.4.4.2 rmind */
295 1.4.4.2 rmind
296 1.4.4.2 rmind /* Make a null-terminated copy of this fan's description. */
297 1.4.4.2 rmind (void)memcpy(name, fan_desc.fd_name, sizeof(fan_desc.fd_name));
298 1.4.4.2 rmind name[sizeof(fan_desc.fd_name)] = '\0';
299 1.4.4.2 rmind
300 1.4.4.2 rmind /* Attach all the sensors for this fan. */
301 1.4.4.2 rmind for (sensor = 0; sensor < __arraycount(fan_sensors); sensor++)
302 1.4.4.2 rmind apple_smc_fan_attach_sensor(sc, fan, name, sensor);
303 1.4.4.2 rmind
304 1.4.4.2 rmind #if 0 /* XXX sysctl */
305 1.4.4.2 rmind /* Attach sysctl knobs to control this fan. */
306 1.4.4.2 rmind apple_smc_fan_sysctl_setup_1(sc, fan);
307 1.4.4.2 rmind #endif
308 1.4.4.2 rmind }
309 1.4.4.2 rmind
310 1.4.4.2 rmind /* Fan sensors are all attached. Register with sysmon_envsys now. */
311 1.4.4.2 rmind error = sysmon_envsys_register(sc->sc_sme);
312 1.4.4.2 rmind if (error)
313 1.4.4.2 rmind goto fail;
314 1.4.4.2 rmind
315 1.4.4.2 rmind /* Success! */
316 1.4.4.2 rmind error = 0;
317 1.4.4.2 rmind goto out;
318 1.4.4.2 rmind
319 1.4.4.2 rmind fail: sysmon_envsys_destroy(sc->sc_sme);
320 1.4.4.2 rmind sc->sc_sme = NULL;
321 1.4.4.2 rmind out: return error;
322 1.4.4.2 rmind }
323 1.4.4.2 rmind
324 1.4.4.2 rmind static void
325 1.4.4.2 rmind apple_smc_fan_attach_sensor(struct apple_smc_fan_softc *sc, uint8_t fan,
326 1.4.4.2 rmind const char *name, uint8_t sensor)
327 1.4.4.2 rmind {
328 1.4.4.2 rmind char key_name[4 + 1];
329 1.4.4.2 rmind struct apple_smc_key **keyp;
330 1.4.4.2 rmind struct envsys_data *edata;
331 1.4.4.2 rmind int error;
332 1.4.4.2 rmind
333 1.4.4.2 rmind KASSERT(fan < sc->sc_nfans);
334 1.4.4.2 rmind KASSERT(sensor < __arraycount(fan_sensors));
335 1.4.4.2 rmind
336 1.4.4.2 rmind /* Format the name of the key for this fan sensor. */
337 1.4.4.2 rmind (void)snprintf(key_name, sizeof(key_name), "F%d%s",
338 1.4.4.2 rmind (int)sensor, fan_sensors[sensor].fs_key_suffix);
339 1.4.4.2 rmind KASSERT(strlen(key_name) == 4);
340 1.4.4.2 rmind
341 1.4.4.2 rmind /* Look up the key for this fan sensor. */
342 1.4.4.2 rmind keyp = &sc->sc_fans[fan].sensors[sensor].sensor_key;
343 1.4.4.2 rmind error = apple_smc_named_key(sc->sc_smc, key_name, APPLE_SMC_TYPE_FPE2,
344 1.4.4.2 rmind keyp);
345 1.4.4.2 rmind if (error)
346 1.4.4.2 rmind goto fail0;
347 1.4.4.2 rmind
348 1.4.4.2 rmind /* Initialize the envsys_data record for this fan sensor. */
349 1.4.4.2 rmind edata = &sc->sc_fans[fan].sensors[sensor].sensor_data;
350 1.4.4.2 rmind edata->units = ENVSYS_SFANRPM;
351 1.4.4.2 rmind edata->state = ENVSYS_SINVALID;
352 1.4.4.2 rmind edata->flags = ENVSYS_FHAS_ENTROPY;
353 1.4.4.2 rmind (void)snprintf(edata->desc, sizeof(edata->desc), "fan %s %s speed",
354 1.4.4.2 rmind name, fan_sensors[sensor].fs_name);
355 1.4.4.2 rmind
356 1.4.4.2 rmind /* Attach this fan sensor to sysmon_envsys. */
357 1.4.4.2 rmind error = sysmon_envsys_sensor_attach(sc->sc_sme, edata);
358 1.4.4.2 rmind if (error)
359 1.4.4.2 rmind goto fail1;
360 1.4.4.2 rmind
361 1.4.4.2 rmind /* Success! */
362 1.4.4.2 rmind return;
363 1.4.4.2 rmind
364 1.4.4.2 rmind fail1: apple_smc_release_key(sc->sc_smc, *keyp);
365 1.4.4.2 rmind fail0: *keyp = NULL;
366 1.4.4.2 rmind aprint_error_dev(sc->sc_dev,
367 1.4.4.2 rmind "failed to attach fan %s %s speed sensor: %d\n",
368 1.4.4.2 rmind name, fan_sensors[sensor].fs_name, error);
369 1.4.4.2 rmind }
370 1.4.4.2 rmind
371 1.4.4.2 rmind static void
372 1.4.4.2 rmind apple_smc_fan_refresh(struct sysmon_envsys *sme, struct envsys_data *edata)
373 1.4.4.2 rmind {
374 1.4.4.2 rmind struct apple_smc_fan_softc *sc = sme->sme_cookie;
375 1.4.4.2 rmind uint8_t fan, sensor;
376 1.4.4.2 rmind struct apple_smc_key *key;
377 1.4.4.2 rmind uint16_t rpm;
378 1.4.4.2 rmind int error;
379 1.4.4.2 rmind
380 1.4.4.2 rmind /* Sanity-check the sensor number out of paranoia. */
381 1.4.4.2 rmind CTASSERT(10 <= (SIZE_MAX / __arraycount(fan_sensors)));
382 1.4.4.2 rmind KASSERT(sc->sc_nfans < 10);
383 1.4.4.2 rmind if (edata->sensor >= (sc->sc_nfans * __arraycount(fan_sensors))) {
384 1.4.4.2 rmind aprint_error_dev(sc->sc_dev, "unknown sensor %"PRIu32"\n",
385 1.4.4.2 rmind edata->sensor);
386 1.4.4.2 rmind return;
387 1.4.4.2 rmind }
388 1.4.4.2 rmind
389 1.4.4.2 rmind /* Pick apart the fan number and its sensor number. */
390 1.4.4.2 rmind fan = (edata->sensor / __arraycount(fan_sensors));
391 1.4.4.2 rmind sensor = (edata->sensor % __arraycount(fan_sensors));
392 1.4.4.2 rmind
393 1.4.4.2 rmind KASSERT(fan < sc->sc_nfans);
394 1.4.4.2 rmind KASSERT(sensor < __arraycount(fan_sensors));
395 1.4.4.2 rmind KASSERT(edata == &sc->sc_fans[fan].sensors[sensor].sensor_data);
396 1.4.4.2 rmind
397 1.4.4.2 rmind /*
398 1.4.4.2 rmind * If we're refreshing, this sensor got attached, so we ought
399 1.4.4.2 rmind * to have a sensor key. Grab it.
400 1.4.4.2 rmind */
401 1.4.4.2 rmind key = sc->sc_fans[fan].sensors[sensor].sensor_key;
402 1.4.4.2 rmind KASSERT(key != NULL);
403 1.4.4.2 rmind
404 1.4.4.2 rmind /* Read the fan sensor value, in rpm. */
405 1.4.4.2 rmind error = apple_smc_read_key_2(sc->sc_smc, key, &rpm);
406 1.4.4.2 rmind if (error) {
407 1.4.4.2 rmind aprint_error_dev(sc->sc_dev,
408 1.4.4.2 rmind "failed to read fan %d %s speed: %d\n",
409 1.4.4.2 rmind fan, fan_sensors[sensor].fs_name, error);
410 1.4.4.2 rmind edata->state = ENVSYS_SINVALID;
411 1.4.4.2 rmind return;
412 1.4.4.2 rmind }
413 1.4.4.2 rmind
414 1.4.4.2 rmind /* Success! */
415 1.4.4.2 rmind edata->value_cur = rpm;
416 1.4.4.2 rmind edata->state = ENVSYS_SVALID;
417 1.4.4.2 rmind }
418 1.4.4.2 rmind
419 1.4.4.2 rmind static void
420 1.4.4.2 rmind apple_smc_fan_release_keys(struct apple_smc_fan_softc *sc)
421 1.4.4.2 rmind {
422 1.4.4.2 rmind uint8_t fan, sensor;
423 1.4.4.2 rmind
424 1.4.4.2 rmind for (fan = 0; fan < sc->sc_nfans; fan++) {
425 1.4.4.2 rmind for (sensor = 0;
426 1.4.4.2 rmind sensor < __arraycount(fan_sensors);
427 1.4.4.2 rmind sensor++) {
428 1.4.4.2 rmind struct apple_smc_key **const keyp =
429 1.4.4.2 rmind &sc->sc_fans[fan].sensors[sensor].sensor_key;
430 1.4.4.2 rmind if (*keyp != NULL) {
431 1.4.4.2 rmind apple_smc_release_key(sc->sc_smc, *keyp);
432 1.4.4.2 rmind *keyp = NULL;
433 1.4.4.2 rmind }
434 1.4.4.2 rmind }
435 1.4.4.2 rmind }
436 1.4.4.2 rmind }
437 1.4.4.2 rmind
438 1.4.4.2 rmind #if 0 /* XXX sysctl */
439 1.4.4.2 rmind static int
440 1.4.4.2 rmind apple_smc_fan_sysctl_setup(struct apple_smc_fan_softc *sc)
441 1.4.4.2 rmind {
442 1.4.4.2 rmind ...
443 1.4.4.2 rmind }
444 1.4.4.2 rmind
445 1.4.4.2 rmind static void
446 1.4.4.2 rmind apple_smc_fan_sysctl_setup_1(struct apple_smc_fan_softc *sc, uint8_t fan)
447 1.4.4.2 rmind {
448 1.4.4.2 rmind }
449 1.4.4.2 rmind #endif
450 1.4.4.2 rmind
451 1.4.4.2 rmind MODULE(MODULE_CLASS_DRIVER, apple_smc_fan, "apple_smc");
452 1.4.4.2 rmind
453 1.4.4.2 rmind #ifdef _MODULE
454 1.4.4.2 rmind #include "ioconf.c"
455 1.4.4.2 rmind #endif
456 1.4.4.2 rmind
457 1.4.4.2 rmind static int
458 1.4.4.2 rmind apple_smc_fan_modcmd(modcmd_t cmd, void *arg __unused)
459 1.4.4.2 rmind {
460 1.4.4.2 rmind #ifdef _MODULE
461 1.4.4.2 rmind int error;
462 1.4.4.2 rmind #endif
463 1.4.4.2 rmind
464 1.4.4.2 rmind switch (cmd) {
465 1.4.4.2 rmind case MODULE_CMD_INIT:
466 1.4.4.2 rmind #ifdef _MODULE
467 1.4.4.2 rmind error = config_init_component(cfdriver_ioconf_apple_smc_fan,
468 1.4.4.2 rmind cfattach_ioconf_apple_smc_fan,
469 1.4.4.2 rmind cfdata_ioconf_apple_smc_fan);
470 1.4.4.2 rmind if (error)
471 1.4.4.2 rmind return error;
472 1.4.4.2 rmind #endif
473 1.4.4.2 rmind return 0;
474 1.4.4.2 rmind
475 1.4.4.2 rmind case MODULE_CMD_FINI:
476 1.4.4.2 rmind #ifdef _MODULE
477 1.4.4.2 rmind error = config_fini_component(cfdriver_ioconf_apple_smc_fan,
478 1.4.4.2 rmind cfattach_ioconf_apple_smc_fan,
479 1.4.4.2 rmind cfdata_ioconf_apple_smc_fan);
480 1.4.4.2 rmind if (error)
481 1.4.4.2 rmind return error;
482 1.4.4.2 rmind #endif
483 1.4.4.2 rmind return 0;
484 1.4.4.2 rmind
485 1.4.4.2 rmind default:
486 1.4.4.2 rmind return ENOTTY;
487 1.4.4.2 rmind }
488 1.4.4.2 rmind }
489