xref: /openbsd/sys/dev/onewire/owid.c (revision 471aeecf)
1*471aeecfSnaddy /*	$OpenBSD: owid.c,v 1.12 2022/04/06 18:59:29 naddy Exp $	*/
2f8c248a6Sgrange 
3f8c248a6Sgrange /*
4f8c248a6Sgrange  * Copyright (c) 2006 Alexander Yurchenko <grange@openbsd.org>
5f8c248a6Sgrange  *
6f8c248a6Sgrange  * Permission to use, copy, modify, and distribute this software for any
7f8c248a6Sgrange  * purpose with or without fee is hereby granted, provided that the above
8f8c248a6Sgrange  * copyright notice and this permission notice appear in all copies.
9f8c248a6Sgrange  *
10f8c248a6Sgrange  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11f8c248a6Sgrange  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12f8c248a6Sgrange  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13f8c248a6Sgrange  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14f8c248a6Sgrange  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15f8c248a6Sgrange  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16f8c248a6Sgrange  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17f8c248a6Sgrange  */
18f8c248a6Sgrange 
19f8c248a6Sgrange /*
20f8c248a6Sgrange  * 1-Wire ID family type device driver.
21f8c248a6Sgrange  */
22f8c248a6Sgrange 
23f8c248a6Sgrange #include <sys/param.h>
24f8c248a6Sgrange #include <sys/systm.h>
25f8c248a6Sgrange #include <sys/device.h>
26f8c248a6Sgrange #include <sys/sensors.h>
27f8c248a6Sgrange 
28f8c248a6Sgrange #include <dev/onewire/onewiredevs.h>
29f8c248a6Sgrange #include <dev/onewire/onewirereg.h>
30f8c248a6Sgrange #include <dev/onewire/onewirevar.h>
31f8c248a6Sgrange 
32f8c248a6Sgrange struct owid_softc {
33f8c248a6Sgrange 	struct device		sc_dev;
34f8c248a6Sgrange 
35f8c248a6Sgrange 	void *			sc_onewire;
36f8c248a6Sgrange 	u_int64_t		sc_rom;
37f8c248a6Sgrange 
38275cbf62Sderaadt 	struct ksensor		sc_sensor;
39275cbf62Sderaadt 	struct ksensordev	sc_sensordev;
40f8c248a6Sgrange 
41f8c248a6Sgrange 	int			sc_dying;
42f8c248a6Sgrange };
43f8c248a6Sgrange 
44f8c248a6Sgrange int	owid_match(struct device *, void *, void *);
45f8c248a6Sgrange void	owid_attach(struct device *, struct device *, void *);
46f8c248a6Sgrange int	owid_detach(struct device *, int);
47e78728c7Spirofti int	owid_activate(struct device *, int);
48f8c248a6Sgrange 
49*471aeecfSnaddy const struct cfattach owid_ca = {
50f8c248a6Sgrange 	sizeof(struct owid_softc),
51f8c248a6Sgrange 	owid_match,
52f8c248a6Sgrange 	owid_attach,
53f8c248a6Sgrange 	owid_detach,
54f8c248a6Sgrange 	owid_activate
55f8c248a6Sgrange };
56f8c248a6Sgrange 
57f8c248a6Sgrange struct cfdriver owid_cd = {
58f8c248a6Sgrange 	NULL, "owid", DV_DULL
59f8c248a6Sgrange };
60f8c248a6Sgrange 
61f8c248a6Sgrange static const struct onewire_matchfam owid_fams[] = {
62f8c248a6Sgrange 	{ ONEWIRE_FAMILY_DS1990 }
63f8c248a6Sgrange };
64f8c248a6Sgrange 
65f8c248a6Sgrange int
owid_match(struct device * parent,void * match,void * aux)66f8c248a6Sgrange owid_match(struct device *parent, void *match, void *aux)
67f8c248a6Sgrange {
68d78896dbSjasper 	return (onewire_matchbyfam(aux, owid_fams, nitems(owid_fams)));
69f8c248a6Sgrange }
70f8c248a6Sgrange 
71f8c248a6Sgrange void
owid_attach(struct device * parent,struct device * self,void * aux)72f8c248a6Sgrange owid_attach(struct device *parent, struct device *self, void *aux)
73f8c248a6Sgrange {
74f8c248a6Sgrange 	struct owid_softc *sc = (struct owid_softc *)self;
75f8c248a6Sgrange 	struct onewire_attach_args *oa = aux;
76f8c248a6Sgrange 
77f8c248a6Sgrange 	sc->sc_onewire = oa->oa_onewire;
78f8c248a6Sgrange 	sc->sc_rom = oa->oa_rom;
79f8c248a6Sgrange 
80f8c248a6Sgrange 	/* Initialize sensor */
8127515a6bSderaadt 	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
8227515a6bSderaadt 	    sizeof(sc->sc_sensordev.xname));
83f8c248a6Sgrange 	sc->sc_sensor.type = SENSOR_INTEGER;
84f8c248a6Sgrange 	sc->sc_sensor.value = ONEWIRE_ROM_SN(sc->sc_rom);
850fb17669Sderaadt 	snprintf(sc->sc_sensor.desc, sizeof(sc->sc_sensor.desc), "sn %012llx",
860fb17669Sderaadt 	    ONEWIRE_ROM_SN(oa->oa_rom));
8727515a6bSderaadt 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
8827515a6bSderaadt 	sensordev_install(&sc->sc_sensordev);
89f8c248a6Sgrange 
90f8c248a6Sgrange 	printf("\n");
91f8c248a6Sgrange }
92f8c248a6Sgrange 
93f8c248a6Sgrange int
owid_detach(struct device * self,int flags)94f8c248a6Sgrange owid_detach(struct device *self, int flags)
95f8c248a6Sgrange {
96f8c248a6Sgrange 	struct owid_softc *sc = (struct owid_softc *)self;
97f8c248a6Sgrange 
9827515a6bSderaadt 	sensordev_deinstall(&sc->sc_sensordev);
99f8c248a6Sgrange 
100f8c248a6Sgrange 	return (0);
101f8c248a6Sgrange }
102f8c248a6Sgrange 
103f8c248a6Sgrange int
owid_activate(struct device * self,int act)104e78728c7Spirofti owid_activate(struct device *self, int act)
105f8c248a6Sgrange {
106f8c248a6Sgrange 	struct owid_softc *sc = (struct owid_softc *)self;
107f8c248a6Sgrange 
108f8c248a6Sgrange 	switch (act) {
109f8c248a6Sgrange 	case DVACT_DEACTIVATE:
110f8c248a6Sgrange 		sc->sc_dying = 1;
111f8c248a6Sgrange 		break;
112f8c248a6Sgrange 	}
113f8c248a6Sgrange 
114f8c248a6Sgrange 	return (0);
115f8c248a6Sgrange }
116