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