xref: /openbsd/sys/dev/onewire/owid.c (revision d78896db)
1 /*	$OpenBSD: owid.c,v 1.8 2010/07/08 07:19:54 jasper 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 *, int);
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, nitems(owid_fams)));
71 }
72 
73 void
74 owid_attach(struct device *parent, struct device *self, void *aux)
75 {
76 	struct owid_softc *sc = (struct owid_softc *)self;
77 	struct onewire_attach_args *oa = aux;
78 
79 	sc->sc_onewire = oa->oa_onewire;
80 	sc->sc_rom = oa->oa_rom;
81 
82 	/* Initialize sensor */
83 	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
84 	    sizeof(sc->sc_sensordev.xname));
85 	sc->sc_sensor.type = SENSOR_INTEGER;
86 	sc->sc_sensor.value = ONEWIRE_ROM_SN(sc->sc_rom);
87 	snprintf(sc->sc_sensor.desc, sizeof(sc->sc_sensor.desc), "sn %012llx",
88 	    ONEWIRE_ROM_SN(oa->oa_rom));
89 	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
90 	sensordev_install(&sc->sc_sensordev);
91 
92 	printf("\n");
93 }
94 
95 int
96 owid_detach(struct device *self, int flags)
97 {
98 	struct owid_softc *sc = (struct owid_softc *)self;
99 
100 	sensordev_deinstall(&sc->sc_sensordev);
101 
102 	return (0);
103 }
104 
105 int
106 owid_activate(struct device *self, int act)
107 {
108 	struct owid_softc *sc = (struct owid_softc *)self;
109 
110 	switch (act) {
111 	case DVACT_ACTIVATE:
112 		break;
113 	case DVACT_DEACTIVATE:
114 		sc->sc_dying = 1;
115 		break;
116 	}
117 
118 	return (0);
119 }
120