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
276
277
278
279
280
281
282
283
284
285
|
/*-
* Copyright (c) 2015 M. Warner Losh <imp@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice unmodified, this list of conditions, and the following
* disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/bus.h>
#include <sys/errno.h>
#include <sys/libkern.h>
#include <sys/kthread.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <sys/sysctl.h>
#include <dev/ow/ow.h>
#include <dev/ow/own.h>
#define OWT_DS1820 0x10 /* Also 18S20 */
#define OWT_DS1822 0x22 /* Very close to 18B20 */
#define OWT_DS18B20 0x28 /* Also MAX31820 */
#define OWT_DS1825 0x3B /* Just like 18B20 with address bits */
#define CONVERT_T 0x44
#define COPY_SCRATCHPAD 0x48
#define WRITE_SCRATCHPAD 0x4e
#define READ_POWER_SUPPLY 0xb4
#define RECALL_EE 0xb8
#define READ_SCRATCHPAD 0xbe
#define OW_TEMP_DONE 0x01
#define OW_TEMP_RUNNING 0x02
struct ow_temp_softc
{
device_t dev;
int type;
int temp;
int flags;
int bad_crc;
int bad_reads;
int reading_interval;
int parasite;
struct mtx temp_lock;
struct proc *event_thread;
};
static int
ow_temp_probe(device_t dev)
{
switch (ow_get_family(dev)) {
case OWT_DS1820:
device_set_desc(dev, "One Wire Temperature");
return BUS_PROBE_DEFAULT;
case OWT_DS1822:
case OWT_DS1825:
case OWT_DS18B20:
device_set_desc(dev, "Advanced One Wire Temperature");
return BUS_PROBE_DEFAULT;
default:
return ENXIO;
}
}
static int
ow_temp_read_scratchpad(device_t dev, uint8_t *scratch, int len)
{
struct ow_cmd cmd;
own_self_command(dev, &cmd, READ_SCRATCHPAD);
cmd.xpt_read_len = len;
own_command_wait(dev, &cmd);
memcpy(scratch, cmd.xpt_read, len);
return 0;
}
static int
ow_temp_convert_t(device_t dev)
{
struct ow_cmd cmd;
own_self_command(dev, &cmd, CONVERT_T);
own_command_wait(dev, &cmd);
return 0;
}
static int
ow_temp_read_power_supply(device_t dev, int *parasite)
{
struct ow_cmd cmd;
own_self_command(dev, &cmd, READ_POWER_SUPPLY);
cmd.flags |= OW_FLAG_READ_BIT;
cmd.xpt_read_len = 1;
own_command_wait(dev, &cmd);
*parasite = !cmd.xpt_read[0]; /* parasites pull bus low */
return 0;
}
static void
ow_temp_event_thread(void *arg)
{
struct ow_temp_softc *sc;
uint8_t scratch[8 + 1];
uint8_t crc;
int retries, rv;
sc = arg;
pause("owtstart", device_get_unit(sc->dev) * hz / 100); // 10ms stagger
mtx_lock(&sc->temp_lock);
sc->flags |= OW_TEMP_RUNNING;
ow_temp_read_power_supply(sc->dev, &sc->parasite);
if (sc->parasite)
device_printf(sc->dev, "Running in parasitic mode unsupported\n");
while ((sc->flags & OW_TEMP_DONE) == 0) {
mtx_unlock(&sc->temp_lock);
ow_temp_convert_t(sc->dev);
mtx_lock(&sc->temp_lock);
msleep(sc, &sc->temp_lock, 0, "owtcvt", hz);
if (sc->flags & OW_TEMP_DONE)
break;
for (retries = 5; retries > 0; retries--) {
mtx_unlock(&sc->temp_lock);
rv = ow_temp_read_scratchpad(sc->dev, scratch, sizeof(scratch));
mtx_lock(&sc->temp_lock);
if (rv == 0) {
crc = own_crc(sc->dev, scratch, sizeof(scratch) - 1);
if (crc == scratch[8]) {
if (sc->type == OWT_DS1820) {
if (scratch[7]) {
/*
* Formula from DS18S20 datasheet, page 6
* DS18S20 datahseet says count_per_c is 16, DS1820 does not
*/
sc->temp = (int16_t)((scratch[0] & 0xfe) |
(scratch[1] << 8)) << 3;
sc->temp += 16 - scratch[6] - 4; /* count_per_c == 16 */
} else
sc->temp = (int16_t)(scratch[0] | (scratch[1] << 8)) << 3;
} else
sc->temp = (int16_t)(scratch[0] | (scratch[1] << 8));
sc->temp = sc->temp * 1000 / 16 + 273150;
break;
}
sc->bad_crc++;
} else
sc->bad_reads++;
}
msleep(sc, &sc->temp_lock, 0, "owtcvt", sc->reading_interval);
}
sc->flags &= ~OW_TEMP_RUNNING;
mtx_unlock(&sc->temp_lock);
kproc_exit(0);
}
static int
ow_temp_attach(device_t dev)
{
struct ow_temp_softc *sc;
sc = device_get_softc(dev);
sc->dev = dev;
sc->type = ow_get_family(dev);
SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
OID_AUTO, "temperature", CTLFLAG_RD | CTLTYPE_INT,
&sc->temp, 0, sysctl_handle_int,
"IK3", "Current Temperature");
SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
OID_AUTO, "badcrc", CTLFLAG_RD,
&sc->bad_crc, 0,
"Number of Bad CRC on reading scratchpad");
SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
OID_AUTO, "badread", CTLFLAG_RD,
&sc->bad_reads, 0,
"Number of errors on reading scratchpad");
SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
OID_AUTO, "reading_interval", CTLFLAG_RW,
&sc->reading_interval, 0,
"ticks between reads");
SYSCTL_ADD_INT(device_get_sysctl_ctx(dev),
SYSCTL_CHILDREN(device_get_sysctl_tree(dev)),
OID_AUTO, "parasite", CTLFLAG_RW,
&sc->parasite, 0,
"In Parasite mode");
/*
* Just do this for unit 0 to avoid locking
* the ow bus until that code can be put
* into place.
*/
sc->temp = -1;
sc->reading_interval = 10 * hz;
mtx_init(&sc->temp_lock, "lock for doing temperature", NULL, MTX_DEF);
/* Start the thread */
if (kproc_create(ow_temp_event_thread, sc, &sc->event_thread, 0, 0,
"%s event thread", device_get_nameunit(dev))) {
device_printf(dev, "unable to create event thread.\n");
panic("cbb_create_event_thread");
}
return 0;
}
static int
ow_temp_detach(device_t dev)
{
struct ow_temp_softc *sc;
sc = device_get_softc(dev);
/*
* Wait for the thread to die. kproc_exit will do a wakeup
* on the event thread's struct thread * so that we know it is
* safe to proceed. IF the thread is running, set the please
* die flag and wait for it to comply. Since the wakeup on
* the event thread happens only in kproc_exit, we don't
* need to loop here.
*/
mtx_lock(&sc->temp_lock);
sc->flags |= OW_TEMP_DONE;
while (sc->flags & OW_TEMP_RUNNING) {
wakeup(sc);
msleep(sc->event_thread, &sc->temp_lock, PWAIT, "owtun", 0);
}
mtx_destroy(&sc->temp_lock);
return 0;
}
devclass_t ow_temp_devclass;
static device_method_t ow_temp_methods[] = {
/* Device interface */
DEVMETHOD(device_probe, ow_temp_probe),
DEVMETHOD(device_attach, ow_temp_attach),
DEVMETHOD(device_detach, ow_temp_detach),
{ 0, 0 }
};
static driver_t ow_temp_driver = {
"ow_temp",
ow_temp_methods,
sizeof(struct ow_temp_softc),
};
DRIVER_MODULE(ow_temp, ow, ow_temp_driver, ow_temp_devclass, 0, 0);
MODULE_DEPEND(ow_temp, ow, 1, 1, 1);
|