onewire.c revision 1.20 1 1.20 kre /* $NetBSD: onewire.c,v 1.20 2020/04/14 15:36:02 kre Exp $ */
2 1.1 riz /* $OpenBSD: onewire.c,v 1.1 2006/03/04 16:27:03 grange Exp $ */
3 1.1 riz
4 1.18 ad /*-
5 1.18 ad * Copyright (c) 2019 The NetBSD Foundation, Inc.
6 1.18 ad * All rights reserved.
7 1.18 ad *
8 1.18 ad * This code is derived from software contributed to The NetBSD Foundation
9 1.18 ad * by Andrew Doran.
10 1.18 ad *
11 1.18 ad * Redistribution and use in source and binary forms, with or without
12 1.18 ad * modification, are permitted provided that the following conditions
13 1.18 ad * are met:
14 1.18 ad * 1. Redistributions of source code must retain the above copyright
15 1.18 ad * notice, this list of conditions and the following disclaimer.
16 1.18 ad * 2. Redistributions in binary form must reproduce the above copyright
17 1.18 ad * notice, this list of conditions and the following disclaimer in the
18 1.18 ad * documentation and/or other materials provided with the distribution.
19 1.18 ad *
20 1.18 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 1.18 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 1.18 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 1.18 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 1.18 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 1.18 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 1.18 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 1.18 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 1.18 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 1.18 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 1.18 ad * POSSIBILITY OF SUCH DAMAGE.
31 1.18 ad */
32 1.18 ad
33 1.1 riz /*
34 1.1 riz * Copyright (c) 2006 Alexander Yurchenko <grange (at) openbsd.org>
35 1.1 riz *
36 1.1 riz * Permission to use, copy, modify, and distribute this software for any
37 1.1 riz * purpose with or without fee is hereby granted, provided that the above
38 1.1 riz * copyright notice and this permission notice appear in all copies.
39 1.1 riz *
40 1.1 riz * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
41 1.1 riz * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42 1.1 riz * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
43 1.1 riz * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44 1.1 riz * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45 1.1 riz * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
46 1.1 riz * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47 1.1 riz */
48 1.1 riz
49 1.1 riz #include <sys/cdefs.h>
50 1.20 kre __KERNEL_RCSID(0, "$NetBSD: onewire.c,v 1.20 2020/04/14 15:36:02 kre Exp $");
51 1.1 riz
52 1.1 riz /*
53 1.1 riz * 1-Wire bus driver.
54 1.1 riz */
55 1.1 riz
56 1.1 riz #include <sys/param.h>
57 1.1 riz #include <sys/systm.h>
58 1.1 riz #include <sys/conf.h>
59 1.1 riz #include <sys/device.h>
60 1.1 riz #include <sys/kernel.h>
61 1.1 riz #include <sys/kthread.h>
62 1.17 martin #include <sys/kmem.h>
63 1.1 riz #include <sys/proc.h>
64 1.1 riz #include <sys/queue.h>
65 1.14 mbalmer #include <sys/module.h>
66 1.1 riz
67 1.20 kre #ifdef _KERNEL_OPT
68 1.19 macallan #include "opt_onewire.h"
69 1.20 kre #endif
70 1.19 macallan
71 1.1 riz #include <dev/onewire/onewirereg.h>
72 1.1 riz #include <dev/onewire/onewirevar.h>
73 1.1 riz
74 1.1 riz #ifdef ONEWIRE_DEBUG
75 1.1 riz #define DPRINTF(x) printf x
76 1.1 riz #else
77 1.1 riz #define DPRINTF(x)
78 1.1 riz #endif
79 1.1 riz
80 1.17 martin int onewire_maxdevs = 8;
81 1.17 martin int onewire_scantime = 10; /* was 3 seconds - too often */
82 1.1 riz
83 1.1 riz struct onewire_softc {
84 1.9 xtraeme device_t sc_dev;
85 1.1 riz struct onewire_bus * sc_bus;
86 1.17 martin kmutex_t sc_lock;
87 1.17 martin kcondvar_t sc_scancv;
88 1.5 ad struct lwp * sc_thread;
89 1.1 riz TAILQ_HEAD(, onewire_device) sc_devs;
90 1.1 riz int sc_dying;
91 1.1 riz };
92 1.1 riz
93 1.1 riz struct onewire_device {
94 1.1 riz TAILQ_ENTRY(onewire_device) d_list;
95 1.9 xtraeme device_t d_dev;
96 1.1 riz u_int64_t d_rom;
97 1.17 martin bool d_present;
98 1.1 riz };
99 1.1 riz
100 1.9 xtraeme static int onewire_match(device_t, cfdata_t, void *);
101 1.9 xtraeme static void onewire_attach(device_t, device_t, void *);
102 1.9 xtraeme static int onewire_detach(device_t, int);
103 1.9 xtraeme static int onewire_activate(device_t, enum devact);
104 1.9 xtraeme int onewire_print(void *, const char *);
105 1.1 riz
106 1.9 xtraeme static void onewire_thread(void *);
107 1.9 xtraeme static void onewire_scan(struct onewire_softc *);
108 1.1 riz
109 1.9 xtraeme CFATTACH_DECL_NEW(onewire, sizeof(struct onewire_softc),
110 1.1 riz onewire_match, onewire_attach, onewire_detach, onewire_activate);
111 1.1 riz
112 1.1 riz extern struct cfdriver onewire_cd;
113 1.1 riz
114 1.9 xtraeme static int
115 1.9 xtraeme onewire_match(device_t parent, cfdata_t cf, void *aux)
116 1.1 riz {
117 1.1 riz return 1;
118 1.1 riz }
119 1.1 riz
120 1.9 xtraeme static void
121 1.9 xtraeme onewire_attach(device_t parent, device_t self, void *aux)
122 1.1 riz {
123 1.1 riz struct onewire_softc *sc = device_private(self);
124 1.1 riz struct onewirebus_attach_args *oba = aux;
125 1.1 riz
126 1.9 xtraeme sc->sc_dev = self;
127 1.1 riz sc->sc_bus = oba->oba_bus;
128 1.17 martin mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
129 1.17 martin cv_init(&sc->sc_scancv, "owscan");
130 1.1 riz TAILQ_INIT(&sc->sc_devs);
131 1.1 riz
132 1.6 xtraeme aprint_normal("\n");
133 1.1 riz
134 1.17 martin if (kthread_create(PRI_NONE, KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL,
135 1.17 martin onewire_thread, sc, &sc->sc_thread, "%s", device_xname(self)) != 0) {
136 1.9 xtraeme aprint_error_dev(self, "can't create kernel thread\n");
137 1.17 martin /* Normally the kthread destroys these. */
138 1.17 martin mutex_destroy(&sc->sc_lock);
139 1.17 martin cv_destroy(&sc->sc_scancv);
140 1.17 martin }
141 1.1 riz }
142 1.1 riz
143 1.9 xtraeme static int
144 1.9 xtraeme onewire_detach(device_t self, int flags)
145 1.1 riz {
146 1.1 riz struct onewire_softc *sc = device_private(self);
147 1.1 riz int rv;
148 1.1 riz
149 1.1 riz if (sc->sc_thread != NULL) {
150 1.17 martin mutex_enter(&sc->sc_lock);
151 1.17 martin sc->sc_dying = 1;
152 1.17 martin cv_broadcast(&sc->sc_scancv);
153 1.17 martin mutex_exit(&sc->sc_lock);
154 1.17 martin /* Must no longer touch sc_lock nor sc_scancv. */
155 1.17 martin kthread_join(sc->sc_thread);
156 1.1 riz }
157 1.1 riz
158 1.1 riz //rv = config_detach_children(self, flags);
159 1.1 riz rv = 0; /* XXX riz */
160 1.1 riz
161 1.6 xtraeme return rv;
162 1.1 riz }
163 1.1 riz
164 1.9 xtraeme static int
165 1.9 xtraeme onewire_activate(device_t self, enum devact act)
166 1.1 riz {
167 1.1 riz struct onewire_softc *sc = device_private(self);
168 1.1 riz
169 1.1 riz switch (act) {
170 1.1 riz case DVACT_DEACTIVATE:
171 1.1 riz sc->sc_dying = 1;
172 1.13 dyoung return 0;
173 1.13 dyoung default:
174 1.13 dyoung return EOPNOTSUPP;
175 1.1 riz }
176 1.1 riz }
177 1.1 riz
178 1.1 riz int
179 1.1 riz onewire_print(void *aux, const char *pnp)
180 1.1 riz {
181 1.1 riz struct onewire_attach_args *oa = aux;
182 1.1 riz const char *famname;
183 1.1 riz
184 1.1 riz if (pnp == NULL)
185 1.6 xtraeme aprint_normal(" ");
186 1.1 riz
187 1.1 riz famname = onewire_famname(ONEWIRE_ROM_FAMILY_TYPE(oa->oa_rom));
188 1.1 riz if (famname == NULL)
189 1.6 xtraeme aprint_normal("family 0x%02x",
190 1.6 xtraeme (uint)ONEWIRE_ROM_FAMILY_TYPE(oa->oa_rom));
191 1.1 riz else
192 1.6 xtraeme aprint_normal("\"%s\"", famname);
193 1.17 martin aprint_normal(" sn %012" PRIx64, ONEWIRE_ROM_SN(oa->oa_rom));
194 1.1 riz
195 1.1 riz if (pnp != NULL)
196 1.6 xtraeme aprint_normal(" at %s", pnp);
197 1.1 riz
198 1.6 xtraeme return UNCONF;
199 1.1 riz }
200 1.1 riz
201 1.1 riz int
202 1.4 christos onewirebus_print(void *aux, const char *pnp)
203 1.1 riz {
204 1.1 riz if (pnp != NULL)
205 1.6 xtraeme aprint_normal("onewire at %s", pnp);
206 1.1 riz
207 1.6 xtraeme return UNCONF;
208 1.1 riz }
209 1.1 riz
210 1.7 xtraeme void
211 1.7 xtraeme onewire_lock(void *arg)
212 1.1 riz {
213 1.1 riz struct onewire_softc *sc = arg;
214 1.1 riz
215 1.17 martin mutex_enter(&sc->sc_lock);
216 1.1 riz }
217 1.1 riz
218 1.1 riz void
219 1.1 riz onewire_unlock(void *arg)
220 1.1 riz {
221 1.1 riz struct onewire_softc *sc = arg;
222 1.1 riz
223 1.17 martin mutex_exit(&sc->sc_lock);
224 1.1 riz }
225 1.1 riz
226 1.1 riz int
227 1.1 riz onewire_reset(void *arg)
228 1.1 riz {
229 1.1 riz struct onewire_softc *sc = arg;
230 1.1 riz struct onewire_bus *bus = sc->sc_bus;
231 1.1 riz
232 1.17 martin KASSERT(mutex_owned(&sc->sc_lock));
233 1.17 martin
234 1.6 xtraeme return bus->bus_reset(bus->bus_cookie);
235 1.1 riz }
236 1.1 riz
237 1.1 riz int
238 1.18 ad onewire_read_bit(void *arg)
239 1.1 riz {
240 1.1 riz struct onewire_softc *sc = arg;
241 1.1 riz struct onewire_bus *bus = sc->sc_bus;
242 1.1 riz
243 1.17 martin KASSERT(mutex_owned(&sc->sc_lock));
244 1.17 martin
245 1.18 ad return bus->bus_read_bit(bus->bus_cookie);
246 1.18 ad }
247 1.18 ad
248 1.18 ad void
249 1.18 ad onewire_write_bit(void *arg, int value)
250 1.18 ad {
251 1.18 ad struct onewire_softc *sc = arg;
252 1.18 ad struct onewire_bus *bus = sc->sc_bus;
253 1.18 ad
254 1.18 ad KASSERT(mutex_owned(&sc->sc_lock));
255 1.18 ad
256 1.18 ad bus->bus_write_bit(bus->bus_cookie, value);
257 1.1 riz }
258 1.1 riz
259 1.1 riz int
260 1.1 riz onewire_read_byte(void *arg)
261 1.1 riz {
262 1.1 riz struct onewire_softc *sc = arg;
263 1.1 riz struct onewire_bus *bus = sc->sc_bus;
264 1.6 xtraeme uint8_t value = 0;
265 1.1 riz int i;
266 1.1 riz
267 1.17 martin KASSERT(mutex_owned(&sc->sc_lock));
268 1.17 martin
269 1.1 riz if (bus->bus_read_byte != NULL)
270 1.6 xtraeme return bus->bus_read_byte(bus->bus_cookie);
271 1.1 riz
272 1.1 riz for (i = 0; i < 8; i++)
273 1.18 ad value |= (bus->bus_read_bit(bus->bus_cookie) << i);
274 1.1 riz
275 1.6 xtraeme return value;
276 1.1 riz }
277 1.1 riz
278 1.1 riz void
279 1.1 riz onewire_write_byte(void *arg, int value)
280 1.1 riz {
281 1.1 riz struct onewire_softc *sc = arg;
282 1.1 riz struct onewire_bus *bus = sc->sc_bus;
283 1.1 riz int i;
284 1.1 riz
285 1.17 martin KASSERT(mutex_owned(&sc->sc_lock));
286 1.17 martin
287 1.1 riz if (bus->bus_write_byte != NULL)
288 1.6 xtraeme return bus->bus_write_byte(bus->bus_cookie, value);
289 1.1 riz
290 1.1 riz for (i = 0; i < 8; i++)
291 1.18 ad bus->bus_write_bit(bus->bus_cookie, (value >> i) & 0x1);
292 1.1 riz }
293 1.1 riz
294 1.1 riz int
295 1.1 riz onewire_triplet(void *arg, int dir)
296 1.1 riz {
297 1.1 riz struct onewire_softc *sc = arg;
298 1.1 riz struct onewire_bus *bus = sc->sc_bus;
299 1.1 riz int rv;
300 1.1 riz
301 1.17 martin KASSERT(mutex_owned(&sc->sc_lock));
302 1.17 martin
303 1.1 riz if (bus->bus_triplet != NULL)
304 1.6 xtraeme return bus->bus_triplet(bus->bus_cookie, dir);
305 1.1 riz
306 1.18 ad rv = bus->bus_read_bit(bus->bus_cookie);
307 1.1 riz rv <<= 1;
308 1.18 ad rv |= bus->bus_read_bit(bus->bus_cookie);
309 1.1 riz
310 1.1 riz switch (rv) {
311 1.1 riz case 0x0:
312 1.18 ad bus->bus_write_bit(bus->bus_cookie, dir);
313 1.1 riz break;
314 1.1 riz case 0x1:
315 1.18 ad bus->bus_write_bit(bus->bus_cookie, 0);
316 1.1 riz break;
317 1.1 riz default:
318 1.18 ad bus->bus_write_bit(bus->bus_cookie, 1);
319 1.1 riz }
320 1.1 riz
321 1.6 xtraeme return rv;
322 1.1 riz }
323 1.1 riz
324 1.1 riz void
325 1.1 riz onewire_read_block(void *arg, void *buf, int len)
326 1.1 riz {
327 1.17 martin struct onewire_softc *sc = arg;
328 1.6 xtraeme uint8_t *p = buf;
329 1.1 riz
330 1.17 martin KASSERT(mutex_owned(&sc->sc_lock));
331 1.17 martin
332 1.1 riz while (len--)
333 1.17 martin *p++ = onewire_read_byte(sc);
334 1.1 riz }
335 1.1 riz
336 1.1 riz void
337 1.1 riz onewire_write_block(void *arg, const void *buf, int len)
338 1.1 riz {
339 1.17 martin struct onewire_softc *sc = arg;
340 1.6 xtraeme const uint8_t *p = buf;
341 1.1 riz
342 1.17 martin KASSERT(mutex_owned(&sc->sc_lock));
343 1.17 martin
344 1.1 riz while (len--)
345 1.17 martin onewire_write_byte(sc, *p++);
346 1.1 riz }
347 1.1 riz
348 1.1 riz void
349 1.1 riz onewire_matchrom(void *arg, u_int64_t rom)
350 1.1 riz {
351 1.17 martin struct onewire_softc *sc = arg;
352 1.1 riz int i;
353 1.1 riz
354 1.17 martin KASSERT(mutex_owned(&sc->sc_lock));
355 1.17 martin
356 1.17 martin onewire_write_byte(sc, ONEWIRE_CMD_MATCH_ROM);
357 1.1 riz for (i = 0; i < 8; i++)
358 1.17 martin onewire_write_byte(sc, (rom >> (i * 8)) & 0xff);
359 1.1 riz }
360 1.1 riz
361 1.9 xtraeme static void
362 1.1 riz onewire_thread(void *arg)
363 1.1 riz {
364 1.1 riz struct onewire_softc *sc = arg;
365 1.18 ad int unit, dly;
366 1.18 ad
367 1.18 ad /*
368 1.18 ad * There can be many onewire busses, potentially funneled through
369 1.18 ad * few GPIO controllers. To avoid a thundering herd of kthreads and
370 1.18 ad * resulting contention for the GPIO controller, spread the probes
371 1.18 ad * out across an 8 second window. The kthreads could converge later
372 1.18 ad * due to timing effects.
373 1.18 ad */
374 1.18 ad unit = device_unit(sc->sc_dev);
375 1.18 ad dly = (unit & 0x07) * hz + ((unit >> 3) * hz >> 3) + 1;
376 1.18 ad (void)kpause("owdly", false, dly, NULL);
377 1.1 riz
378 1.17 martin mutex_enter(&sc->sc_lock);
379 1.1 riz while (!sc->sc_dying) {
380 1.1 riz onewire_scan(sc);
381 1.17 martin (void)cv_timedwait(&sc->sc_scancv, &sc->sc_lock,
382 1.17 martin onewire_scantime * hz);
383 1.1 riz }
384 1.17 martin mutex_exit(&sc->sc_lock);
385 1.1 riz
386 1.17 martin /* Caller has set sc_dying and will no longer touch these. */
387 1.17 martin cv_destroy(&sc->sc_scancv);
388 1.17 martin mutex_destroy(&sc->sc_lock);
389 1.1 riz kthread_exit(0);
390 1.1 riz }
391 1.9 xtraeme
392 1.9 xtraeme static void
393 1.1 riz onewire_scan(struct onewire_softc *sc)
394 1.1 riz {
395 1.1 riz struct onewire_device *d, *next, *nd;
396 1.1 riz struct onewire_attach_args oa;
397 1.1 riz int search = 1, count = 0, present;
398 1.1 riz int dir, rv;
399 1.6 xtraeme uint64_t mask, rom = 0, lastrom;
400 1.6 xtraeme uint8_t data[8];
401 1.1 riz int i, i0 = -1, lastd = -1;
402 1.1 riz
403 1.17 martin TAILQ_FOREACH(d, &sc->sc_devs, d_list) {
404 1.17 martin d->d_present = false;
405 1.17 martin KASSERT(d->d_dev != NULL);
406 1.17 martin }
407 1.1 riz
408 1.17 martin KASSERT(mutex_owned(&sc->sc_lock));
409 1.17 martin KASSERT(curlwp == sc->sc_thread);
410 1.1 riz
411 1.17 martin while (search && count++ < onewire_maxdevs) {
412 1.1 riz /*
413 1.18 ad * Reset the bus, allowing for one retry if reset fails. If
414 1.18 ad * there's no presence pulse don't search for any devices.
415 1.1 riz */
416 1.1 riz if (onewire_reset(sc) != 0) {
417 1.1 riz DPRINTF(("%s: scan: no presence pulse\n",
418 1.9 xtraeme device_xname(sc->sc_dev)));
419 1.18 ad if (onewire_reset(sc) != 0) {
420 1.18 ad DPRINTF(("%s: scan: retry failed\n",
421 1.18 ad device_xname(sc->sc_dev)));
422 1.18 ad break;
423 1.18 ad }
424 1.1 riz }
425 1.1 riz
426 1.1 riz /*
427 1.1 riz * Start new search. Go through the previous path to
428 1.1 riz * the point we made a decision last time and make an
429 1.1 riz * opposite decision. If we didn't make any decision
430 1.1 riz * stop searching.
431 1.1 riz */
432 1.1 riz search = 0;
433 1.1 riz lastrom = rom;
434 1.1 riz rom = 0;
435 1.1 riz onewire_write_byte(sc, ONEWIRE_CMD_SEARCH_ROM);
436 1.1 riz for (i = 0,i0 = -1; i < 64; i++) {
437 1.1 riz dir = (lastrom >> i) & 0x1;
438 1.1 riz if (i == lastd)
439 1.1 riz dir = 1;
440 1.1 riz else if (i > lastd)
441 1.1 riz dir = 0;
442 1.1 riz rv = onewire_triplet(sc, dir);
443 1.1 riz switch (rv) {
444 1.1 riz case 0x0:
445 1.1 riz if (i != lastd) {
446 1.1 riz if (dir == 0)
447 1.1 riz i0 = i;
448 1.1 riz search = 1;
449 1.1 riz }
450 1.1 riz mask = dir;
451 1.1 riz break;
452 1.1 riz case 0x1:
453 1.1 riz mask = 0;
454 1.1 riz break;
455 1.1 riz case 0x2:
456 1.1 riz mask = 1;
457 1.1 riz break;
458 1.1 riz default:
459 1.1 riz DPRINTF(("%s: scan: triplet error 0x%x, "
460 1.1 riz "step %d\n",
461 1.9 xtraeme device_xname(sc->sc_dev), rv, i));
462 1.1 riz return;
463 1.1 riz }
464 1.1 riz rom |= (mask << i);
465 1.1 riz }
466 1.1 riz lastd = i0;
467 1.1 riz
468 1.18 ad /*
469 1.18 ad * Yield processor, but continue to hold the lock
470 1.18 ad * so that scan is not interrupted.
471 1.18 ad */
472 1.18 ad (void)kpause("owscan", false, 1, NULL);
473 1.18 ad
474 1.1 riz if (rom == 0)
475 1.1 riz continue;
476 1.1 riz
477 1.1 riz /*
478 1.1 riz * The last byte of the ROM code contains a CRC calculated
479 1.1 riz * from the first 7 bytes. Re-calculate it to make sure
480 1.1 riz * we found a valid device.
481 1.1 riz */
482 1.1 riz for (i = 0; i < 8; i++)
483 1.1 riz data[i] = (rom >> (i * 8)) & 0xff;
484 1.1 riz if (onewire_crc(data, 7) != data[7])
485 1.1 riz continue;
486 1.1 riz
487 1.1 riz /*
488 1.1 riz * Go through the list of attached devices to see if we
489 1.1 riz * found a new one.
490 1.1 riz */
491 1.1 riz present = 0;
492 1.14 mbalmer TAILQ_FOREACH(d, &sc->sc_devs, d_list) {
493 1.1 riz if (d->d_rom == rom) {
494 1.17 martin d->d_present = true;
495 1.1 riz present = 1;
496 1.1 riz break;
497 1.1 riz }
498 1.1 riz }
499 1.1 riz if (!present) {
500 1.17 martin nd = kmem_alloc(sizeof(*nd), KM_SLEEP);
501 1.17 martin nd->d_dev = NULL;
502 1.1 riz nd->d_rom = rom;
503 1.17 martin nd->d_present = true;
504 1.1 riz TAILQ_INSERT_TAIL(&sc->sc_devs, nd, d_list);
505 1.1 riz }
506 1.1 riz }
507 1.1 riz
508 1.17 martin /*
509 1.17 martin * Detach disappeared devices, and attach new devices. Drop the
510 1.17 martin * lock when doing this in order to prevent lock order reversal
511 1.17 martin * against sysmon. This is safe because nothing other than this
512 1.17 martin * kthread modifies our device list.
513 1.17 martin */
514 1.17 martin for (d = TAILQ_FIRST(&sc->sc_devs); d != NULL; d = next) {
515 1.1 riz next = TAILQ_NEXT(d, d_list);
516 1.1 riz if (!d->d_present) {
517 1.17 martin mutex_exit(&sc->sc_lock);
518 1.17 martin
519 1.17 martin KERNEL_LOCK(1, NULL); /* XXXSMP */
520 1.1 riz config_detach(d->d_dev, DETACH_FORCE);
521 1.17 martin d->d_dev = NULL;
522 1.17 martin KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
523 1.17 martin
524 1.17 martin mutex_enter(&sc->sc_lock);
525 1.17 martin } else if (d->d_dev == NULL) {
526 1.17 martin memset(&oa, 0, sizeof(oa));
527 1.17 martin oa.oa_onewire = sc;
528 1.17 martin oa.oa_rom = d->d_rom;
529 1.17 martin mutex_exit(&sc->sc_lock);
530 1.17 martin
531 1.17 martin KERNEL_LOCK(1, NULL); /* XXXSMP */
532 1.17 martin d->d_dev = config_found(sc->sc_dev, &oa, onewire_print);
533 1.17 martin KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
534 1.17 martin
535 1.17 martin mutex_enter(&sc->sc_lock);
536 1.17 martin }
537 1.17 martin if (d->d_dev == NULL) {
538 1.1 riz TAILQ_REMOVE(&sc->sc_devs, d, d_list);
539 1.17 martin kmem_free(d, sizeof(*d));
540 1.1 riz }
541 1.1 riz }
542 1.1 riz }
543 1.14 mbalmer
544 1.14 mbalmer MODULE(MODULE_CLASS_DRIVER, onewire, NULL);
545 1.14 mbalmer
546 1.14 mbalmer #ifdef _MODULE
547 1.14 mbalmer #include "ioconf.c"
548 1.14 mbalmer #endif
549 1.14 mbalmer
550 1.14 mbalmer static int
551 1.14 mbalmer onewire_modcmd(modcmd_t cmd, void *opaque)
552 1.14 mbalmer {
553 1.14 mbalmer int error;
554 1.14 mbalmer
555 1.14 mbalmer error = 0;
556 1.14 mbalmer switch (cmd) {
557 1.14 mbalmer case MODULE_CMD_INIT:
558 1.14 mbalmer #ifdef _MODULE
559 1.14 mbalmer error = config_init_component(cfdriver_ioconf_onewire,
560 1.14 mbalmer cfattach_ioconf_onewire, cfdata_ioconf_onewire);
561 1.14 mbalmer if (error)
562 1.14 mbalmer aprint_error("%s: unable to init component\n",
563 1.14 mbalmer onewire_cd.cd_name);
564 1.14 mbalmer #endif
565 1.14 mbalmer break;
566 1.14 mbalmer case MODULE_CMD_FINI:
567 1.14 mbalmer #ifdef _MODULE
568 1.14 mbalmer config_fini_component(cfdriver_ioconf_onewire,
569 1.14 mbalmer cfattach_ioconf_onewire, cfdata_ioconf_onewire);
570 1.14 mbalmer #endif
571 1.14 mbalmer break;
572 1.14 mbalmer default:
573 1.14 mbalmer error = ENOTTY;
574 1.14 mbalmer }
575 1.14 mbalmer return error;
576 1.14 mbalmer }
577