xref: /openbsd/sys/dev/onewire/owid.c (revision e78728c7)
1*e78728c7Spirofti /*	$OpenBSD: owid.c,v 1.7 2009/10/13 19:33:16 pirofti 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/kernel.h>
27f8c248a6Sgrange #include <sys/proc.h>
28f8c248a6Sgrange #include <sys/sensors.h>
29f8c248a6Sgrange 
30f8c248a6Sgrange #include <dev/onewire/onewiredevs.h>
31f8c248a6Sgrange #include <dev/onewire/onewirereg.h>
32f8c248a6Sgrange #include <dev/onewire/onewirevar.h>
33f8c248a6Sgrange 
34f8c248a6Sgrange struct owid_softc {
35f8c248a6Sgrange 	struct device		sc_dev;
36f8c248a6Sgrange 
37f8c248a6Sgrange 	void *			sc_onewire;
38f8c248a6Sgrange 	u_int64_t		sc_rom;
39f8c248a6Sgrange 
40275cbf62Sderaadt 	struct ksensor		sc_sensor;
41275cbf62Sderaadt 	struct ksensordev	sc_sensordev;
42f8c248a6Sgrange 
43f8c248a6Sgrange 	int			sc_dying;
44f8c248a6Sgrange };
45f8c248a6Sgrange 
46f8c248a6Sgrange int	owid_match(struct device *, void *, void *);
47f8c248a6Sgrange void	owid_attach(struct device *, struct device *, void *);
48f8c248a6Sgrange int	owid_detach(struct device *, int);
49*e78728c7Spirofti int	owid_activate(struct device *, int);
50f8c248a6Sgrange 
51f8c248a6Sgrange struct cfattach owid_ca = {
52f8c248a6Sgrange 	sizeof(struct owid_softc),
53f8c248a6Sgrange 	owid_match,
54f8c248a6Sgrange 	owid_attach,
55f8c248a6Sgrange 	owid_detach,
56f8c248a6Sgrange 	owid_activate
57f8c248a6Sgrange };
58f8c248a6Sgrange 
59f8c248a6Sgrange struct cfdriver owid_cd = {
60f8c248a6Sgrange 	NULL, "owid", DV_DULL
61f8c248a6Sgrange };
62f8c248a6Sgrange 
63f8c248a6Sgrange static const struct onewire_matchfam owid_fams[] = {
64f8c248a6Sgrange 	{ ONEWIRE_FAMILY_DS1990 }
65f8c248a6Sgrange };
66f8c248a6Sgrange 
67f8c248a6Sgrange int
68f8c248a6Sgrange owid_match(struct device *parent, void *match, void *aux)
69f8c248a6Sgrange {
70f8c248a6Sgrange 	return (onewire_matchbyfam(aux, owid_fams,
71f8c248a6Sgrange 	    sizeof(owid_fams) /sizeof(owid_fams[0])));
72f8c248a6Sgrange }
73f8c248a6Sgrange 
74f8c248a6Sgrange void
75f8c248a6Sgrange owid_attach(struct device *parent, struct device *self, void *aux)
76f8c248a6Sgrange {
77f8c248a6Sgrange 	struct owid_softc *sc = (struct owid_softc *)self;
78f8c248a6Sgrange 	struct onewire_attach_args *oa = aux;
79f8c248a6Sgrange 
80f8c248a6Sgrange 	sc->sc_onewire = oa->oa_onewire;
81f8c248a6Sgrange 	sc->sc_rom = oa->oa_rom;
82f8c248a6Sgrange 
83f8c248a6Sgrange 	/* Initialize sensor */
8427515a6bSderaadt 	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
8527515a6bSderaadt 	    sizeof(sc->sc_sensordev.xname));
86f8c248a6Sgrange 	sc->sc_sensor.type = SENSOR_INTEGER;
87f8c248a6Sgrange 	sc->sc_sensor.value = ONEWIRE_ROM_SN(sc->sc_rom);
880fb17669Sderaadt 	snprintf(sc->sc_sensor.desc, sizeof(sc->sc_sensor.desc), "sn %012llx",
890fb17669Sderaadt 	    ONEWIRE_ROM_SN(oa->oa_rom));
9027515a6bSderaadt 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
9127515a6bSderaadt 	sensordev_install(&sc->sc_sensordev);
92f8c248a6Sgrange 
93f8c248a6Sgrange 	printf("\n");
94f8c248a6Sgrange }
95f8c248a6Sgrange 
96f8c248a6Sgrange int
97f8c248a6Sgrange owid_detach(struct device *self, int flags)
98f8c248a6Sgrange {
99f8c248a6Sgrange 	struct owid_softc *sc = (struct owid_softc *)self;
100f8c248a6Sgrange 
10127515a6bSderaadt 	sensordev_deinstall(&sc->sc_sensordev);
102f8c248a6Sgrange 
103f8c248a6Sgrange 	return (0);
104f8c248a6Sgrange }
105f8c248a6Sgrange 
106f8c248a6Sgrange int
107*e78728c7Spirofti owid_activate(struct device *self, int act)
108f8c248a6Sgrange {
109f8c248a6Sgrange 	struct owid_softc *sc = (struct owid_softc *)self;
110f8c248a6Sgrange 
111f8c248a6Sgrange 	switch (act) {
112f8c248a6Sgrange 	case DVACT_ACTIVATE:
11307aaebccSmiod 		break;
114f8c248a6Sgrange 	case DVACT_DEACTIVATE:
115f8c248a6Sgrange 		sc->sc_dying = 1;
116f8c248a6Sgrange 		break;
117f8c248a6Sgrange 	}
118f8c248a6Sgrange 
119f8c248a6Sgrange 	return (0);
120f8c248a6Sgrange }
121