1/*	$OpenBSD: owid.c,v 1.12 2022/04/06 18:59:29 naddy 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/sensors.h>
27
28#include <dev/onewire/onewiredevs.h>
29#include <dev/onewire/onewirereg.h>
30#include <dev/onewire/onewirevar.h>
31
32struct owid_softc {
33	struct device		sc_dev;
34
35	void *			sc_onewire;
36	u_int64_t		sc_rom;
37
38	struct ksensor		sc_sensor;
39	struct ksensordev	sc_sensordev;
40
41	int			sc_dying;
42};
43
44int	owid_match(struct device *, void *, void *);
45void	owid_attach(struct device *, struct device *, void *);
46int	owid_detach(struct device *, int);
47int	owid_activate(struct device *, int);
48
49const struct cfattach owid_ca = {
50	sizeof(struct owid_softc),
51	owid_match,
52	owid_attach,
53	owid_detach,
54	owid_activate
55};
56
57struct cfdriver owid_cd = {
58	NULL, "owid", DV_DULL
59};
60
61static const struct onewire_matchfam owid_fams[] = {
62	{ ONEWIRE_FAMILY_DS1990 }
63};
64
65int
66owid_match(struct device *parent, void *match, void *aux)
67{
68	return (onewire_matchbyfam(aux, owid_fams, nitems(owid_fams)));
69}
70
71void
72owid_attach(struct device *parent, struct device *self, void *aux)
73{
74	struct owid_softc *sc = (struct owid_softc *)self;
75	struct onewire_attach_args *oa = aux;
76
77	sc->sc_onewire = oa->oa_onewire;
78	sc->sc_rom = oa->oa_rom;
79
80	/* Initialize sensor */
81	strlcpy(sc->sc_sensordev.xname, sc->sc_dev.dv_xname,
82	    sizeof(sc->sc_sensordev.xname));
83	sc->sc_sensor.type = SENSOR_INTEGER;
84	sc->sc_sensor.value = ONEWIRE_ROM_SN(sc->sc_rom);
85	snprintf(sc->sc_sensor.desc, sizeof(sc->sc_sensor.desc), "sn %012llx",
86	    ONEWIRE_ROM_SN(oa->oa_rom));
87	sensor_attach(&sc->sc_sensordev, &sc->sc_sensor);
88	sensordev_install(&sc->sc_sensordev);
89
90	printf("\n");
91}
92
93int
94owid_detach(struct device *self, int flags)
95{
96	struct owid_softc *sc = (struct owid_softc *)self;
97
98	sensordev_deinstall(&sc->sc_sensordev);
99
100	return (0);
101}
102
103int
104owid_activate(struct device *self, int act)
105{
106	struct owid_softc *sc = (struct owid_softc *)self;
107
108	switch (act) {
109	case DVACT_DEACTIVATE:
110		sc->sc_dying = 1;
111		break;
112	}
113
114	return (0);
115}
116