1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/* $OpenBSD: kern_sensors.c,v 1.40 2022/12/05 23:18:37 deraadt Exp $ */
/*
* Copyright (c) 2005 David Gwynne <dlg@openbsd.org>
* Copyright (c) 2006 Constantine A. Murenin <cnst+openbsd@bugmail.mojo.ru>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/malloc.h>
#include <sys/queue.h>
#include <sys/device.h>
#include <sys/hotplug.h>
#include <sys/timeout.h>
#include <sys/task.h>
#include <sys/rwlock.h>
#include <sys/atomic.h>
#include <sys/sensors.h>
#include "hotplug.h"
struct taskq *sensors_taskq;
int sensordev_count;
SLIST_HEAD(, ksensordev) sensordev_list =
SLIST_HEAD_INITIALIZER(sensordev_list);
void
sensordev_install(struct ksensordev *sensdev)
{
struct ksensordev *v, *nv;
int s;
s = splhigh();
if (sensordev_count == 0) {
sensdev->num = 0;
SLIST_INSERT_HEAD(&sensordev_list, sensdev, list);
} else {
for (v = SLIST_FIRST(&sensordev_list);
(nv = SLIST_NEXT(v, list)) != NULL; v = nv)
if (nv->num - v->num > 1)
break;
sensdev->num = v->num + 1;
SLIST_INSERT_AFTER(v, sensdev, list);
}
sensordev_count++;
splx(s);
#if NHOTPLUG > 0
hotplug_device_attach(DV_DULL, "sensordev");
#endif
}
void
sensor_attach(struct ksensordev *sensdev, struct ksensor *sens)
{
struct ksensor *v, *nv;
struct ksensors_head *sh;
int s, i;
s = splhigh();
sh = &sensdev->sensors_list;
if (sensdev->sensors_count == 0) {
for (i = 0; i < SENSOR_MAX_TYPES; i++)
sensdev->maxnumt[i] = 0;
sens->numt = 0;
SLIST_INSERT_HEAD(sh, sens, list);
} else {
for (v = SLIST_FIRST(sh);
(nv = SLIST_NEXT(v, list)) != NULL; v = nv)
if (v->type == sens->type && (v->type != nv->type ||
(v->type == nv->type && nv->numt - v->numt > 1)))
break;
/* sensors of the same type go after each other */
if (v->type == sens->type)
sens->numt = v->numt + 1;
else
sens->numt = 0;
SLIST_INSERT_AFTER(v, sens, list);
}
/* we only increment maxnumt[] if the sensor was added
* to the last position of sensors of this type
*/
if (sensdev->maxnumt[sens->type] == sens->numt)
sensdev->maxnumt[sens->type]++;
sensdev->sensors_count++;
splx(s);
}
void
sensordev_deinstall(struct ksensordev *sensdev)
{
int s;
s = splhigh();
sensordev_count--;
SLIST_REMOVE(&sensordev_list, sensdev, ksensordev, list);
splx(s);
#if NHOTPLUG > 0
hotplug_device_detach(DV_DULL, "sensordev");
#endif
}
void
sensor_detach(struct ksensordev *sensdev, struct ksensor *sens)
{
struct ksensors_head *sh;
int s;
s = splhigh();
sh = &sensdev->sensors_list;
sensdev->sensors_count--;
SLIST_REMOVE(sh, sens, ksensor, list);
/* we only decrement maxnumt[] if this is the tail
* sensor of this type
*/
if (sens->numt == sensdev->maxnumt[sens->type] - 1)
sensdev->maxnumt[sens->type]--;
splx(s);
}
int
sensordev_get(int num, struct ksensordev **sensdev)
{
struct ksensordev *sd;
SLIST_FOREACH(sd, &sensordev_list, list) {
if (sd->num == num) {
*sensdev = sd;
return (0);
}
if (sd->num > num)
return (ENXIO);
}
return (ENOENT);
}
int
sensor_find(int dev, enum sensor_type type, int numt, struct ksensor **ksensorp)
{
struct ksensor *s;
struct ksensordev *sensdev;
struct ksensors_head *sh;
int ret;
ret = sensordev_get(dev, &sensdev);
if (ret)
return (ret);
sh = &sensdev->sensors_list;
SLIST_FOREACH(s, sh, list)
if (s->type == type && s->numt == numt) {
*ksensorp = s;
return (0);
}
return (ENOENT);
}
struct sensor_task {
void (*func)(void *);
void *arg;
unsigned int period;
struct timeout timeout;
struct task task;
struct rwlock lock;
};
void sensor_task_tick(void *);
void sensor_task_work(void *);
struct sensor_task *
sensor_task_register(void *arg, void (*func)(void *), unsigned int period)
{
struct sensor_task *st;
#ifdef DIAGNOSTIC
if (period == 0)
panic("sensor_task_register: period is 0");
#endif
if (sensors_taskq == NULL &&
(sensors_taskq = taskq_create("sensors", 1, IPL_HIGH, 0)) == NULL)
sensors_taskq = systq;
st = malloc(sizeof(*st), M_DEVBUF, M_NOWAIT);
if (st == NULL)
return (NULL);
st->func = func;
st->arg = arg;
st->period = period;
timeout_set(&st->timeout, sensor_task_tick, st);
task_set(&st->task, sensor_task_work, st);
rw_init(&st->lock, "sensor");
sensor_task_tick(st);
return (st);
}
void
sensor_task_unregister(struct sensor_task *st)
{
/*
* we can't reliably timeout_del or task_del because there's a window
* between when they come off the lists and the timeout or task code
* actually runs the respective handlers for them. mark the sensor_task
* as dying by setting period to 0 and let sensor_task_work mop up.
*/
rw_enter_write(&st->lock);
st->period = 0;
rw_exit_write(&st->lock);
}
void
sensor_task_tick(void *arg)
{
struct sensor_task *st = arg;
task_add(sensors_taskq, &st->task);
}
static int sensors_quiesced;
static int sensors_running;
void
sensor_quiesce(void)
{
sensors_quiesced = 1;
while (sensors_running > 0)
tsleep_nsec(&sensors_running, PZERO, "sensorpause", INFSLP);
}
void
sensor_restart(void)
{
sensors_quiesced = 0;
}
void
sensor_task_work(void *xst)
{
struct sensor_task *st = xst;
unsigned int period = 0;
atomic_inc_int(&sensors_running);
rw_enter_write(&st->lock);
period = st->period;
if (period > 0 && !sensors_quiesced)
st->func(st->arg);
rw_exit_write(&st->lock);
if (atomic_dec_int_nv(&sensors_running) == 0) {
if (sensors_quiesced)
wakeup(&sensors_running);
}
if (period == 0)
free(st, M_DEVBUF, sizeof(*st));
else
timeout_add_sec(&st->timeout, period);
}
|