This source file includes following definitions.
- owtemp_match
- owtemp_attach
- owtemp_detach
- owtemp_activate
- owtemp_update
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/device.h>
26 #include <sys/kernel.h>
27 #include <sys/proc.h>
28 #include <sys/rwlock.h>
29 #include <sys/sensors.h>
30
31 #include <dev/onewire/onewiredevs.h>
32 #include <dev/onewire/onewirereg.h>
33 #include <dev/onewire/onewirevar.h>
34
35
36 #define DS1920_CMD_CONVERT 0x44
37 #define DS1920_CMD_READ_SCRATCHPAD 0xbe
38
39
40 #define DS1920_SP_TEMP_LSB 0
41 #define DS1920_SP_TEMP_MSB 1
42 #define DS1920_SP_TH 2
43 #define DS1920_SP_TL 3
44 #define DS1920_SP_COUNT_REMAIN 6
45 #define DS1920_SP_COUNT_PERC 7
46 #define DS1920_SP_CRC 8
47
48 struct owtemp_softc {
49 struct device sc_dev;
50
51 void * sc_onewire;
52 u_int64_t sc_rom;
53
54 struct ksensor sc_sensor;
55 struct ksensordev sc_sensordev;
56 struct sensor_task *sc_sensortask;
57 struct rwlock sc_lock;
58 };
59
60 int owtemp_match(struct device *, void *, void *);
61 void owtemp_attach(struct device *, struct device *, void *);
62 int owtemp_detach(struct device *, int);
63 int owtemp_activate(struct device *, enum devact);
64
65 void owtemp_update(void *);
66
67 struct cfattach owtemp_ca = {
68 sizeof(struct owtemp_softc),
69 owtemp_match,
70 owtemp_attach,
71 owtemp_detach,
72 owtemp_activate
73 };
74
75 struct cfdriver owtemp_cd = {
76 NULL, "owtemp", DV_DULL
77 };
78
79 static const struct onewire_matchfam owtemp_fams[] = {
80 { ONEWIRE_FAMILY_DS1920 }
81 };
82
83 int
84 owtemp_match(struct device *parent, void *match, void *aux)
85 {
86 return (onewire_matchbyfam(aux, owtemp_fams,
87 sizeof(owtemp_fams) /sizeof(owtemp_fams[0])));
88 }
89
90 void
91 owtemp_attach(struct device *parent, struct device *self, void *aux)
92 {
93 struct owtemp_softc *sc = (struct owtemp_softc *)self;
94 struct onewire_attach_args *oa = aux;
95
96 sc->sc_onewire = oa->oa_onewire;
97 sc->sc_rom = oa->oa_rom;
98
99
100 strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
101 sizeof(sc->sc_sensordev.xname));
102 sc->sc_sensor.type = SENSOR_TEMP;
103
104 sc->sc_sensortask = sensor_task_register(sc, owtemp_update, 5);
105 if (sc->sc_sensortask == NULL) {
106 printf(": unable to register update task\n");
107 return;
108 }
109 sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
110 sensordev_install(&sc->sc_sensordev);
111
112 rw_init(&sc->sc_lock, sc->sc_dev.dv_xname);
113 printf("\n");
114 }
115
116 int
117 owtemp_detach(struct device *self, int flags)
118 {
119 struct owtemp_softc *sc = (struct owtemp_softc *)self;
120
121 rw_enter_write(&sc->sc_lock);
122 sensordev_deinstall(&sc->sc_sensordev);
123 if (sc->sc_sensortask != NULL)
124 sensor_task_unregister(sc->sc_sensortask);
125 rw_exit_write(&sc->sc_lock);
126
127 return (0);
128 }
129
130 int
131 owtemp_activate(struct device *self, enum devact act)
132 {
133 return (0);
134 }
135
136 void
137 owtemp_update(void *arg)
138 {
139 struct owtemp_softc *sc = arg;
140 u_int8_t data[9];
141 u_int16_t temp;
142 int count_perc, count_remain, val;
143
144 rw_enter_write(&sc->sc_lock);
145 onewire_lock(sc->sc_onewire, 0);
146 if (onewire_reset(sc->sc_onewire) != 0)
147 goto done;
148 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
149
150
151
152
153
154
155
156
157 onewire_write_byte(sc->sc_onewire, DS1920_CMD_CONVERT);
158 tsleep(sc, PRIBIO, "owtemp", hz);
159
160 if (onewire_reset(sc->sc_onewire) != 0)
161 goto done;
162 onewire_matchrom(sc->sc_onewire, sc->sc_rom);
163
164
165
166
167
168 onewire_write_byte(sc->sc_onewire, DS1920_CMD_READ_SCRATCHPAD);
169 onewire_read_block(sc->sc_onewire, data, 9);
170 if (onewire_crc(data, 8) == data[DS1920_SP_CRC]) {
171 temp = data[DS1920_SP_TEMP_MSB] << 8 |
172 data[DS1920_SP_TEMP_LSB];
173 count_perc = data[DS1920_SP_COUNT_PERC];
174 count_remain = data[DS1920_SP_COUNT_REMAIN];
175
176 if (count_perc != 0) {
177
178 temp &= ~0x0001;
179 val = temp * 500000 - 250000 +
180 ((count_perc - count_remain) * 1000000) /
181 count_perc;
182 } else {
183 val = temp * 500000;
184 }
185 sc->sc_sensor.value = 273150000 + val;
186 }
187
188 done:
189 onewire_unlock(sc->sc_onewire);
190 rw_exit_write(&sc->sc_lock);
191 }