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