1*c7fb772bSthorpej /* $NetBSD: onewire.c,v 1.22 2021/08/07 16:19:14 thorpej Exp $ */
2637bfc29Sriz /* $OpenBSD: onewire.c,v 1.1 2006/03/04 16:27:03 grange Exp $ */
3637bfc29Sriz
4e7a559b6Sad /*-
5e7a559b6Sad * Copyright (c) 2019 The NetBSD Foundation, Inc.
6e7a559b6Sad * All rights reserved.
7e7a559b6Sad *
8e7a559b6Sad * This code is derived from software contributed to The NetBSD Foundation
9e7a559b6Sad * by Andrew Doran.
10e7a559b6Sad *
11e7a559b6Sad * Redistribution and use in source and binary forms, with or without
12e7a559b6Sad * modification, are permitted provided that the following conditions
13e7a559b6Sad * are met:
14e7a559b6Sad * 1. Redistributions of source code must retain the above copyright
15e7a559b6Sad * notice, this list of conditions and the following disclaimer.
16e7a559b6Sad * 2. Redistributions in binary form must reproduce the above copyright
17e7a559b6Sad * notice, this list of conditions and the following disclaimer in the
18e7a559b6Sad * documentation and/or other materials provided with the distribution.
19e7a559b6Sad *
20e7a559b6Sad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21e7a559b6Sad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22e7a559b6Sad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23e7a559b6Sad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24e7a559b6Sad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25e7a559b6Sad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26e7a559b6Sad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27e7a559b6Sad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28e7a559b6Sad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29e7a559b6Sad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30e7a559b6Sad * POSSIBILITY OF SUCH DAMAGE.
31e7a559b6Sad */
32e7a559b6Sad
33637bfc29Sriz /*
34637bfc29Sriz * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
35637bfc29Sriz *
36637bfc29Sriz * Permission to use, copy, modify, and distribute this software for any
37637bfc29Sriz * purpose with or without fee is hereby granted, provided that the above
38637bfc29Sriz * copyright notice and this permission notice appear in all copies.
39637bfc29Sriz *
40637bfc29Sriz * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
41637bfc29Sriz * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
42637bfc29Sriz * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
43637bfc29Sriz * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
44637bfc29Sriz * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
45637bfc29Sriz * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
46637bfc29Sriz * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
47637bfc29Sriz */
48637bfc29Sriz
49637bfc29Sriz #include <sys/cdefs.h>
50*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: onewire.c,v 1.22 2021/08/07 16:19:14 thorpej Exp $");
51637bfc29Sriz
52637bfc29Sriz /*
53637bfc29Sriz * 1-Wire bus driver.
54637bfc29Sriz */
55637bfc29Sriz
56637bfc29Sriz #include <sys/param.h>
57637bfc29Sriz #include <sys/systm.h>
58637bfc29Sriz #include <sys/conf.h>
59637bfc29Sriz #include <sys/device.h>
60637bfc29Sriz #include <sys/kernel.h>
61637bfc29Sriz #include <sys/kthread.h>
62c8f8ec1fSmartin #include <sys/kmem.h>
63637bfc29Sriz #include <sys/proc.h>
64637bfc29Sriz #include <sys/queue.h>
65c16a4100Smbalmer #include <sys/module.h>
66637bfc29Sriz
67d7771cb4Skre #ifdef _KERNEL_OPT
6843dafa47Smacallan #include "opt_onewire.h"
69d7771cb4Skre #endif
7043dafa47Smacallan
71637bfc29Sriz #include <dev/onewire/onewirereg.h>
72637bfc29Sriz #include <dev/onewire/onewirevar.h>
73637bfc29Sriz
74637bfc29Sriz #ifdef ONEWIRE_DEBUG
75637bfc29Sriz #define DPRINTF(x) printf x
76637bfc29Sriz #else
77637bfc29Sriz #define DPRINTF(x)
78637bfc29Sriz #endif
79637bfc29Sriz
80c8f8ec1fSmartin int onewire_maxdevs = 8;
81c8f8ec1fSmartin int onewire_scantime = 10; /* was 3 seconds - too often */
82637bfc29Sriz
83637bfc29Sriz struct onewire_softc {
84f14b65c7Sxtraeme device_t sc_dev;
85637bfc29Sriz struct onewire_bus * sc_bus;
86c8f8ec1fSmartin kmutex_t sc_lock;
87c8f8ec1fSmartin kcondvar_t sc_scancv;
8888ab7da9Sad struct lwp * sc_thread;
89637bfc29Sriz TAILQ_HEAD(, onewire_device) sc_devs;
90637bfc29Sriz int sc_dying;
91637bfc29Sriz };
92637bfc29Sriz
93637bfc29Sriz struct onewire_device {
94637bfc29Sriz TAILQ_ENTRY(onewire_device) d_list;
95f14b65c7Sxtraeme device_t d_dev;
96637bfc29Sriz u_int64_t d_rom;
97c8f8ec1fSmartin bool d_present;
98637bfc29Sriz };
99637bfc29Sriz
100f14b65c7Sxtraeme static int onewire_match(device_t, cfdata_t, void *);
101f14b65c7Sxtraeme static void onewire_attach(device_t, device_t, void *);
102f14b65c7Sxtraeme static int onewire_detach(device_t, int);
103f14b65c7Sxtraeme static int onewire_activate(device_t, enum devact);
104637bfc29Sriz int onewire_print(void *, const char *);
105637bfc29Sriz
106f14b65c7Sxtraeme static void onewire_thread(void *);
107f14b65c7Sxtraeme static void onewire_scan(struct onewire_softc *);
108637bfc29Sriz
109f14b65c7Sxtraeme CFATTACH_DECL_NEW(onewire, sizeof(struct onewire_softc),
110637bfc29Sriz onewire_match, onewire_attach, onewire_detach, onewire_activate);
111637bfc29Sriz
112637bfc29Sriz extern struct cfdriver onewire_cd;
113637bfc29Sriz
114f14b65c7Sxtraeme static int
onewire_match(device_t parent,cfdata_t cf,void * aux)115f14b65c7Sxtraeme onewire_match(device_t parent, cfdata_t cf, void *aux)
116637bfc29Sriz {
117637bfc29Sriz return 1;
118637bfc29Sriz }
119637bfc29Sriz
120f14b65c7Sxtraeme static void
onewire_attach(device_t parent,device_t self,void * aux)121f14b65c7Sxtraeme onewire_attach(device_t parent, device_t self, void *aux)
122637bfc29Sriz {
123637bfc29Sriz struct onewire_softc *sc = device_private(self);
124637bfc29Sriz struct onewirebus_attach_args *oba = aux;
125637bfc29Sriz
126f14b65c7Sxtraeme sc->sc_dev = self;
127637bfc29Sriz sc->sc_bus = oba->oba_bus;
128c8f8ec1fSmartin mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
129c8f8ec1fSmartin cv_init(&sc->sc_scancv, "owscan");
130637bfc29Sriz TAILQ_INIT(&sc->sc_devs);
131637bfc29Sriz
1320b4713a2Sxtraeme aprint_normal("\n");
133637bfc29Sriz
134c8f8ec1fSmartin if (kthread_create(PRI_NONE, KTHREAD_MUSTJOIN | KTHREAD_MPSAFE, NULL,
135c8f8ec1fSmartin onewire_thread, sc, &sc->sc_thread, "%s", device_xname(self)) != 0) {
136f14b65c7Sxtraeme aprint_error_dev(self, "can't create kernel thread\n");
137c8f8ec1fSmartin /* Normally the kthread destroys these. */
138c8f8ec1fSmartin mutex_destroy(&sc->sc_lock);
139c8f8ec1fSmartin cv_destroy(&sc->sc_scancv);
140c8f8ec1fSmartin }
141637bfc29Sriz }
142637bfc29Sriz
143f14b65c7Sxtraeme static int
onewire_detach(device_t self,int flags)144f14b65c7Sxtraeme onewire_detach(device_t self, int flags)
145637bfc29Sriz {
146637bfc29Sriz struct onewire_softc *sc = device_private(self);
147637bfc29Sriz int rv;
148637bfc29Sriz
149637bfc29Sriz if (sc->sc_thread != NULL) {
150c8f8ec1fSmartin mutex_enter(&sc->sc_lock);
151c8f8ec1fSmartin sc->sc_dying = 1;
152c8f8ec1fSmartin cv_broadcast(&sc->sc_scancv);
153c8f8ec1fSmartin mutex_exit(&sc->sc_lock);
154c8f8ec1fSmartin /* Must no longer touch sc_lock nor sc_scancv. */
155c8f8ec1fSmartin kthread_join(sc->sc_thread);
156637bfc29Sriz }
157637bfc29Sriz
158637bfc29Sriz //rv = config_detach_children(self, flags);
159637bfc29Sriz rv = 0; /* XXX riz */
160637bfc29Sriz
1610b4713a2Sxtraeme return rv;
162637bfc29Sriz }
163637bfc29Sriz
164f14b65c7Sxtraeme static int
onewire_activate(device_t self,enum devact act)165f14b65c7Sxtraeme onewire_activate(device_t self, enum devact act)
166637bfc29Sriz {
167637bfc29Sriz struct onewire_softc *sc = device_private(self);
168637bfc29Sriz
169637bfc29Sriz switch (act) {
170637bfc29Sriz case DVACT_DEACTIVATE:
171637bfc29Sriz sc->sc_dying = 1;
172c6747de6Sdyoung return 0;
173c6747de6Sdyoung default:
174c6747de6Sdyoung return EOPNOTSUPP;
175637bfc29Sriz }
176637bfc29Sriz }
177637bfc29Sriz
178637bfc29Sriz int
onewire_print(void * aux,const char * pnp)179637bfc29Sriz onewire_print(void *aux, const char *pnp)
180637bfc29Sriz {
181637bfc29Sriz struct onewire_attach_args *oa = aux;
182637bfc29Sriz const char *famname;
183637bfc29Sriz
184637bfc29Sriz if (pnp == NULL)
1850b4713a2Sxtraeme aprint_normal(" ");
186637bfc29Sriz
187637bfc29Sriz famname = onewire_famname(ONEWIRE_ROM_FAMILY_TYPE(oa->oa_rom));
188637bfc29Sriz if (famname == NULL)
1890b4713a2Sxtraeme aprint_normal("family 0x%02x",
1900b4713a2Sxtraeme (uint)ONEWIRE_ROM_FAMILY_TYPE(oa->oa_rom));
191637bfc29Sriz else
1920b4713a2Sxtraeme aprint_normal("\"%s\"", famname);
193c8f8ec1fSmartin aprint_normal(" sn %012" PRIx64, ONEWIRE_ROM_SN(oa->oa_rom));
194637bfc29Sriz
195637bfc29Sriz if (pnp != NULL)
1960b4713a2Sxtraeme aprint_normal(" at %s", pnp);
197637bfc29Sriz
1980b4713a2Sxtraeme return UNCONF;
199637bfc29Sriz }
200637bfc29Sriz
201637bfc29Sriz int
onewirebus_print(void * aux,const char * pnp)202168cd830Schristos onewirebus_print(void *aux, const char *pnp)
203637bfc29Sriz {
204637bfc29Sriz if (pnp != NULL)
2050b4713a2Sxtraeme aprint_normal("onewire at %s", pnp);
206637bfc29Sriz
2070b4713a2Sxtraeme return UNCONF;
208637bfc29Sriz }
209637bfc29Sriz
210f5ad5900Sxtraeme void
onewire_lock(void * arg)211f5ad5900Sxtraeme onewire_lock(void *arg)
212637bfc29Sriz {
213637bfc29Sriz struct onewire_softc *sc = arg;
214637bfc29Sriz
215c8f8ec1fSmartin mutex_enter(&sc->sc_lock);
216637bfc29Sriz }
217637bfc29Sriz
218637bfc29Sriz void
onewire_unlock(void * arg)219637bfc29Sriz onewire_unlock(void *arg)
220637bfc29Sriz {
221637bfc29Sriz struct onewire_softc *sc = arg;
222637bfc29Sriz
223c8f8ec1fSmartin mutex_exit(&sc->sc_lock);
224637bfc29Sriz }
225637bfc29Sriz
226637bfc29Sriz int
onewire_reset(void * arg)227637bfc29Sriz onewire_reset(void *arg)
228637bfc29Sriz {
229637bfc29Sriz struct onewire_softc *sc = arg;
230637bfc29Sriz struct onewire_bus *bus = sc->sc_bus;
231637bfc29Sriz
232c8f8ec1fSmartin KASSERT(mutex_owned(&sc->sc_lock));
233c8f8ec1fSmartin
2340b4713a2Sxtraeme return bus->bus_reset(bus->bus_cookie);
235637bfc29Sriz }
236637bfc29Sriz
237637bfc29Sriz int
onewire_read_bit(void * arg)238e7a559b6Sad onewire_read_bit(void *arg)
239637bfc29Sriz {
240637bfc29Sriz struct onewire_softc *sc = arg;
241637bfc29Sriz struct onewire_bus *bus = sc->sc_bus;
242637bfc29Sriz
243c8f8ec1fSmartin KASSERT(mutex_owned(&sc->sc_lock));
244c8f8ec1fSmartin
245e7a559b6Sad return bus->bus_read_bit(bus->bus_cookie);
246e7a559b6Sad }
247e7a559b6Sad
248e7a559b6Sad void
onewire_write_bit(void * arg,int value)249e7a559b6Sad onewire_write_bit(void *arg, int value)
250e7a559b6Sad {
251e7a559b6Sad struct onewire_softc *sc = arg;
252e7a559b6Sad struct onewire_bus *bus = sc->sc_bus;
253e7a559b6Sad
254e7a559b6Sad KASSERT(mutex_owned(&sc->sc_lock));
255e7a559b6Sad
256e7a559b6Sad bus->bus_write_bit(bus->bus_cookie, value);
257637bfc29Sriz }
258637bfc29Sriz
259637bfc29Sriz int
onewire_read_byte(void * arg)260637bfc29Sriz onewire_read_byte(void *arg)
261637bfc29Sriz {
262637bfc29Sriz struct onewire_softc *sc = arg;
263637bfc29Sriz struct onewire_bus *bus = sc->sc_bus;
2640b4713a2Sxtraeme uint8_t value = 0;
265637bfc29Sriz int i;
266637bfc29Sriz
267c8f8ec1fSmartin KASSERT(mutex_owned(&sc->sc_lock));
268c8f8ec1fSmartin
269637bfc29Sriz if (bus->bus_read_byte != NULL)
2700b4713a2Sxtraeme return bus->bus_read_byte(bus->bus_cookie);
271637bfc29Sriz
272637bfc29Sriz for (i = 0; i < 8; i++)
273e7a559b6Sad value |= (bus->bus_read_bit(bus->bus_cookie) << i);
274637bfc29Sriz
2750b4713a2Sxtraeme return value;
276637bfc29Sriz }
277637bfc29Sriz
278637bfc29Sriz void
onewire_write_byte(void * arg,int value)279637bfc29Sriz onewire_write_byte(void *arg, int value)
280637bfc29Sriz {
281637bfc29Sriz struct onewire_softc *sc = arg;
282637bfc29Sriz struct onewire_bus *bus = sc->sc_bus;
283637bfc29Sriz int i;
284637bfc29Sriz
285c8f8ec1fSmartin KASSERT(mutex_owned(&sc->sc_lock));
286c8f8ec1fSmartin
287637bfc29Sriz if (bus->bus_write_byte != NULL)
2880b4713a2Sxtraeme return bus->bus_write_byte(bus->bus_cookie, value);
289637bfc29Sriz
290637bfc29Sriz for (i = 0; i < 8; i++)
291e7a559b6Sad bus->bus_write_bit(bus->bus_cookie, (value >> i) & 0x1);
292637bfc29Sriz }
293637bfc29Sriz
294637bfc29Sriz int
onewire_triplet(void * arg,int dir)295637bfc29Sriz onewire_triplet(void *arg, int dir)
296637bfc29Sriz {
297637bfc29Sriz struct onewire_softc *sc = arg;
298637bfc29Sriz struct onewire_bus *bus = sc->sc_bus;
299637bfc29Sriz int rv;
300637bfc29Sriz
301c8f8ec1fSmartin KASSERT(mutex_owned(&sc->sc_lock));
302c8f8ec1fSmartin
303637bfc29Sriz if (bus->bus_triplet != NULL)
3040b4713a2Sxtraeme return bus->bus_triplet(bus->bus_cookie, dir);
305637bfc29Sriz
306e7a559b6Sad rv = bus->bus_read_bit(bus->bus_cookie);
307637bfc29Sriz rv <<= 1;
308e7a559b6Sad rv |= bus->bus_read_bit(bus->bus_cookie);
309637bfc29Sriz
310637bfc29Sriz switch (rv) {
311637bfc29Sriz case 0x0:
312e7a559b6Sad bus->bus_write_bit(bus->bus_cookie, dir);
313637bfc29Sriz break;
314637bfc29Sriz case 0x1:
315e7a559b6Sad bus->bus_write_bit(bus->bus_cookie, 0);
316637bfc29Sriz break;
317637bfc29Sriz default:
318e7a559b6Sad bus->bus_write_bit(bus->bus_cookie, 1);
319637bfc29Sriz }
320637bfc29Sriz
3210b4713a2Sxtraeme return rv;
322637bfc29Sriz }
323637bfc29Sriz
324637bfc29Sriz void
onewire_read_block(void * arg,void * buf,int len)325637bfc29Sriz onewire_read_block(void *arg, void *buf, int len)
326637bfc29Sriz {
327c8f8ec1fSmartin struct onewire_softc *sc = arg;
3280b4713a2Sxtraeme uint8_t *p = buf;
329637bfc29Sriz
330c8f8ec1fSmartin KASSERT(mutex_owned(&sc->sc_lock));
331c8f8ec1fSmartin
332637bfc29Sriz while (len--)
333c8f8ec1fSmartin *p++ = onewire_read_byte(sc);
334637bfc29Sriz }
335637bfc29Sriz
336637bfc29Sriz void
onewire_write_block(void * arg,const void * buf,int len)337637bfc29Sriz onewire_write_block(void *arg, const void *buf, int len)
338637bfc29Sriz {
339c8f8ec1fSmartin struct onewire_softc *sc = arg;
3400b4713a2Sxtraeme const uint8_t *p = buf;
341637bfc29Sriz
342c8f8ec1fSmartin KASSERT(mutex_owned(&sc->sc_lock));
343c8f8ec1fSmartin
344637bfc29Sriz while (len--)
345c8f8ec1fSmartin onewire_write_byte(sc, *p++);
346637bfc29Sriz }
347637bfc29Sriz
348637bfc29Sriz void
onewire_matchrom(void * arg,u_int64_t rom)349637bfc29Sriz onewire_matchrom(void *arg, u_int64_t rom)
350637bfc29Sriz {
351c8f8ec1fSmartin struct onewire_softc *sc = arg;
352637bfc29Sriz int i;
353637bfc29Sriz
354c8f8ec1fSmartin KASSERT(mutex_owned(&sc->sc_lock));
355c8f8ec1fSmartin
356c8f8ec1fSmartin onewire_write_byte(sc, ONEWIRE_CMD_MATCH_ROM);
357637bfc29Sriz for (i = 0; i < 8; i++)
358c8f8ec1fSmartin onewire_write_byte(sc, (rom >> (i * 8)) & 0xff);
359637bfc29Sriz }
360637bfc29Sriz
361f14b65c7Sxtraeme static void
onewire_thread(void * arg)362637bfc29Sriz onewire_thread(void *arg)
363637bfc29Sriz {
364637bfc29Sriz struct onewire_softc *sc = arg;
365e7a559b6Sad int unit, dly;
366e7a559b6Sad
367e7a559b6Sad /*
368e7a559b6Sad * There can be many onewire busses, potentially funneled through
369e7a559b6Sad * few GPIO controllers. To avoid a thundering herd of kthreads and
370e7a559b6Sad * resulting contention for the GPIO controller, spread the probes
371e7a559b6Sad * out across an 8 second window. The kthreads could converge later
372e7a559b6Sad * due to timing effects.
373e7a559b6Sad */
374e7a559b6Sad unit = device_unit(sc->sc_dev);
375e7a559b6Sad dly = (unit & 0x07) * hz + ((unit >> 3) * hz >> 3) + 1;
376e7a559b6Sad (void)kpause("owdly", false, dly, NULL);
377637bfc29Sriz
378c8f8ec1fSmartin mutex_enter(&sc->sc_lock);
379637bfc29Sriz while (!sc->sc_dying) {
380637bfc29Sriz onewire_scan(sc);
381c8f8ec1fSmartin (void)cv_timedwait(&sc->sc_scancv, &sc->sc_lock,
382c8f8ec1fSmartin onewire_scantime * hz);
383637bfc29Sriz }
384c8f8ec1fSmartin mutex_exit(&sc->sc_lock);
385637bfc29Sriz
386c8f8ec1fSmartin /* Caller has set sc_dying and will no longer touch these. */
387c8f8ec1fSmartin cv_destroy(&sc->sc_scancv);
388c8f8ec1fSmartin mutex_destroy(&sc->sc_lock);
389637bfc29Sriz kthread_exit(0);
390637bfc29Sriz }
391f14b65c7Sxtraeme
392f14b65c7Sxtraeme static void
onewire_scan(struct onewire_softc * sc)393637bfc29Sriz onewire_scan(struct onewire_softc *sc)
394637bfc29Sriz {
395637bfc29Sriz struct onewire_device *d, *next, *nd;
396637bfc29Sriz struct onewire_attach_args oa;
397637bfc29Sriz int search = 1, count = 0, present;
398637bfc29Sriz int dir, rv;
3990b4713a2Sxtraeme uint64_t mask, rom = 0, lastrom;
4000b4713a2Sxtraeme uint8_t data[8];
401637bfc29Sriz int i, i0 = -1, lastd = -1;
402637bfc29Sriz
403c8f8ec1fSmartin TAILQ_FOREACH(d, &sc->sc_devs, d_list) {
404c8f8ec1fSmartin d->d_present = false;
405c8f8ec1fSmartin KASSERT(d->d_dev != NULL);
406c8f8ec1fSmartin }
407637bfc29Sriz
408c8f8ec1fSmartin KASSERT(mutex_owned(&sc->sc_lock));
409c8f8ec1fSmartin KASSERT(curlwp == sc->sc_thread);
410637bfc29Sriz
411c8f8ec1fSmartin while (search && count++ < onewire_maxdevs) {
412637bfc29Sriz /*
413e7a559b6Sad * Reset the bus, allowing for one retry if reset fails. If
414e7a559b6Sad * there's no presence pulse don't search for any devices.
415637bfc29Sriz */
416637bfc29Sriz if (onewire_reset(sc) != 0) {
417637bfc29Sriz DPRINTF(("%s: scan: no presence pulse\n",
418f14b65c7Sxtraeme device_xname(sc->sc_dev)));
419e7a559b6Sad if (onewire_reset(sc) != 0) {
420e7a559b6Sad DPRINTF(("%s: scan: retry failed\n",
421e7a559b6Sad device_xname(sc->sc_dev)));
422637bfc29Sriz break;
423637bfc29Sriz }
424e7a559b6Sad }
425637bfc29Sriz
426637bfc29Sriz /*
427637bfc29Sriz * Start new search. Go through the previous path to
428637bfc29Sriz * the point we made a decision last time and make an
429637bfc29Sriz * opposite decision. If we didn't make any decision
430637bfc29Sriz * stop searching.
431637bfc29Sriz */
432637bfc29Sriz search = 0;
433637bfc29Sriz lastrom = rom;
434637bfc29Sriz rom = 0;
435637bfc29Sriz onewire_write_byte(sc, ONEWIRE_CMD_SEARCH_ROM);
436637bfc29Sriz for (i = 0,i0 = -1; i < 64; i++) {
437637bfc29Sriz dir = (lastrom >> i) & 0x1;
438637bfc29Sriz if (i == lastd)
439637bfc29Sriz dir = 1;
440637bfc29Sriz else if (i > lastd)
441637bfc29Sriz dir = 0;
442637bfc29Sriz rv = onewire_triplet(sc, dir);
443637bfc29Sriz switch (rv) {
444637bfc29Sriz case 0x0:
445637bfc29Sriz if (i != lastd) {
446637bfc29Sriz if (dir == 0)
447637bfc29Sriz i0 = i;
448637bfc29Sriz search = 1;
449637bfc29Sriz }
450637bfc29Sriz mask = dir;
451637bfc29Sriz break;
452637bfc29Sriz case 0x1:
453637bfc29Sriz mask = 0;
454637bfc29Sriz break;
455637bfc29Sriz case 0x2:
456637bfc29Sriz mask = 1;
457637bfc29Sriz break;
458637bfc29Sriz default:
459637bfc29Sriz DPRINTF(("%s: scan: triplet error 0x%x, "
460637bfc29Sriz "step %d\n",
461f14b65c7Sxtraeme device_xname(sc->sc_dev), rv, i));
462637bfc29Sriz return;
463637bfc29Sriz }
464637bfc29Sriz rom |= (mask << i);
465637bfc29Sriz }
466637bfc29Sriz lastd = i0;
467637bfc29Sriz
468e7a559b6Sad /*
469e7a559b6Sad * Yield processor, but continue to hold the lock
470e7a559b6Sad * so that scan is not interrupted.
471e7a559b6Sad */
472e7a559b6Sad (void)kpause("owscan", false, 1, NULL);
473e7a559b6Sad
474637bfc29Sriz if (rom == 0)
475637bfc29Sriz continue;
476637bfc29Sriz
477637bfc29Sriz /*
478637bfc29Sriz * The last byte of the ROM code contains a CRC calculated
479637bfc29Sriz * from the first 7 bytes. Re-calculate it to make sure
480637bfc29Sriz * we found a valid device.
481637bfc29Sriz */
482637bfc29Sriz for (i = 0; i < 8; i++)
483637bfc29Sriz data[i] = (rom >> (i * 8)) & 0xff;
484637bfc29Sriz if (onewire_crc(data, 7) != data[7])
485637bfc29Sriz continue;
486637bfc29Sriz
487637bfc29Sriz /*
488637bfc29Sriz * Go through the list of attached devices to see if we
489637bfc29Sriz * found a new one.
490637bfc29Sriz */
491637bfc29Sriz present = 0;
492637bfc29Sriz TAILQ_FOREACH(d, &sc->sc_devs, d_list) {
493637bfc29Sriz if (d->d_rom == rom) {
494c8f8ec1fSmartin d->d_present = true;
495637bfc29Sriz present = 1;
496637bfc29Sriz break;
497637bfc29Sriz }
498637bfc29Sriz }
499637bfc29Sriz if (!present) {
500c8f8ec1fSmartin nd = kmem_alloc(sizeof(*nd), KM_SLEEP);
501c8f8ec1fSmartin nd->d_dev = NULL;
502637bfc29Sriz nd->d_rom = rom;
503c8f8ec1fSmartin nd->d_present = true;
504637bfc29Sriz TAILQ_INSERT_TAIL(&sc->sc_devs, nd, d_list);
505637bfc29Sriz }
506637bfc29Sriz }
507637bfc29Sriz
508c8f8ec1fSmartin /*
509c8f8ec1fSmartin * Detach disappeared devices, and attach new devices. Drop the
510c8f8ec1fSmartin * lock when doing this in order to prevent lock order reversal
511c8f8ec1fSmartin * against sysmon. This is safe because nothing other than this
512c8f8ec1fSmartin * kthread modifies our device list.
513c8f8ec1fSmartin */
514c8f8ec1fSmartin for (d = TAILQ_FIRST(&sc->sc_devs); d != NULL; d = next) {
515637bfc29Sriz next = TAILQ_NEXT(d, d_list);
516637bfc29Sriz if (!d->d_present) {
517c8f8ec1fSmartin mutex_exit(&sc->sc_lock);
518c8f8ec1fSmartin
519c8f8ec1fSmartin KERNEL_LOCK(1, NULL); /* XXXSMP */
520637bfc29Sriz config_detach(d->d_dev, DETACH_FORCE);
521c8f8ec1fSmartin d->d_dev = NULL;
522c8f8ec1fSmartin KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
523c8f8ec1fSmartin
524c8f8ec1fSmartin mutex_enter(&sc->sc_lock);
525c8f8ec1fSmartin } else if (d->d_dev == NULL) {
526c8f8ec1fSmartin memset(&oa, 0, sizeof(oa));
527c8f8ec1fSmartin oa.oa_onewire = sc;
528c8f8ec1fSmartin oa.oa_rom = d->d_rom;
529c8f8ec1fSmartin mutex_exit(&sc->sc_lock);
530c8f8ec1fSmartin
531c8f8ec1fSmartin KERNEL_LOCK(1, NULL); /* XXXSMP */
5322685996bSthorpej d->d_dev = config_found(sc->sc_dev, &oa, onewire_print,
533*c7fb772bSthorpej CFARGS_NONE);
534c8f8ec1fSmartin KERNEL_UNLOCK_ONE(NULL); /* XXXSMP */
535c8f8ec1fSmartin
536c8f8ec1fSmartin mutex_enter(&sc->sc_lock);
537c8f8ec1fSmartin }
538c8f8ec1fSmartin if (d->d_dev == NULL) {
539637bfc29Sriz TAILQ_REMOVE(&sc->sc_devs, d, d_list);
540c8f8ec1fSmartin kmem_free(d, sizeof(*d));
541637bfc29Sriz }
542637bfc29Sriz }
543637bfc29Sriz }
544c16a4100Smbalmer
545c16a4100Smbalmer MODULE(MODULE_CLASS_DRIVER, onewire, NULL);
546c16a4100Smbalmer
547c16a4100Smbalmer #ifdef _MODULE
548c16a4100Smbalmer #include "ioconf.c"
549c16a4100Smbalmer #endif
550c16a4100Smbalmer
551c16a4100Smbalmer static int
onewire_modcmd(modcmd_t cmd,void * opaque)552c16a4100Smbalmer onewire_modcmd(modcmd_t cmd, void *opaque)
553c16a4100Smbalmer {
554c16a4100Smbalmer int error;
555c16a4100Smbalmer
556c16a4100Smbalmer error = 0;
557c16a4100Smbalmer switch (cmd) {
558c16a4100Smbalmer case MODULE_CMD_INIT:
559c16a4100Smbalmer #ifdef _MODULE
560c16a4100Smbalmer error = config_init_component(cfdriver_ioconf_onewire,
561c16a4100Smbalmer cfattach_ioconf_onewire, cfdata_ioconf_onewire);
562c16a4100Smbalmer if (error)
563c16a4100Smbalmer aprint_error("%s: unable to init component\n",
564c16a4100Smbalmer onewire_cd.cd_name);
565c16a4100Smbalmer #endif
566c16a4100Smbalmer break;
567c16a4100Smbalmer case MODULE_CMD_FINI:
568c16a4100Smbalmer #ifdef _MODULE
569c16a4100Smbalmer config_fini_component(cfdriver_ioconf_onewire,
570c16a4100Smbalmer cfattach_ioconf_onewire, cfdata_ioconf_onewire);
571c16a4100Smbalmer #endif
572c16a4100Smbalmer break;
573c16a4100Smbalmer default:
574c16a4100Smbalmer error = ENOTTY;
575c16a4100Smbalmer }
576c16a4100Smbalmer return error;
577c16a4100Smbalmer }
578