1/*-
2 * Copyright (c) 1998, 2001 Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 *
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/10/sys/dev/smbus/smbus.c 308042 2016-10-28 15:02:24Z avg $");
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/lock.h>
34#include <sys/malloc.h>
35#include <sys/module.h>
36#include <sys/mutex.h>
37#include <sys/bus.h>
38
39#include <dev/smbus/smbconf.h>
40#include <dev/smbus/smbus.h>
41
42
43struct smbus_ivar
44{
45	uint8_t	addr;
46};
47
48/*
49 * Autoconfiguration and support routines for System Management bus
50 */
51
52static int
53smbus_probe(device_t dev)
54{
55
56	device_set_desc(dev, "System Management Bus");
57
58	return (0);
59}
60
61static int
62smbus_attach(device_t dev)
63{
64	struct smbus_softc *sc = device_get_softc(dev);
65
66	mtx_init(&sc->lock, device_get_nameunit(dev), "smbus", MTX_DEF);
67	bus_generic_probe(dev);
68	bus_enumerate_hinted_children(dev);
69	bus_generic_attach(dev);
70
71	return (0);
72}
73
74static int
75smbus_detach(device_t dev)
76{
77	struct smbus_softc *sc = device_get_softc(dev);
78	int error;
79
80	error = bus_generic_detach(dev);
81	if (error)
82		return (error);
83	device_delete_children(dev);
84	mtx_destroy(&sc->lock);
85
86	return (0);
87}
88
89void
90smbus_generic_intr(device_t dev, u_char devaddr, char low, char high, int err)
91{
92}
93
94static device_t
95smbus_add_child(device_t dev, u_int order, const char *name, int unit)
96{
97	struct smbus_ivar *devi;
98	device_t child;
99
100	child = device_add_child_ordered(dev, order, name, unit);
101	if (child == NULL)
102		return (child);
103	devi = malloc(sizeof(struct smbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO);
104	if (devi == NULL) {
105		device_delete_child(dev, child);
106		return (NULL);
107	}
108	device_set_ivars(child, devi);
109	return (child);
110}
111
112static void
113smbus_hinted_child(device_t bus, const char *dname, int dunit)
114{
115	struct smbus_ivar *devi;
116	device_t child;
117	int addr;
118
119	addr = 0;
120	resource_int_value(dname, dunit, "addr", &addr);
121	if (addr > UINT8_MAX) {
122		device_printf(bus, "ignored incorrect slave address hint 0x%x"
123		    " for %s%d\n", addr, dname, dunit);
124		return;
125	}
126	child = BUS_ADD_CHILD(bus, SMBUS_ORDER_HINTED, dname, dunit);
127	if (child == NULL)
128		return;
129	devi = device_get_ivars(child);
130	devi->addr = addr;
131}
132
133
134static int
135smbus_child_location_str(device_t parent, device_t child, char *buf,
136    size_t buflen)
137{
138	struct smbus_ivar *devi;
139
140	devi = device_get_ivars(child);
141	if (devi->addr != 0)
142		snprintf(buf, buflen, "addr=0x%x", devi->addr);
143	else if (buflen)
144		buf[0] = 0;
145	return (0);
146}
147
148static int
149smbus_print_child(device_t parent, device_t child)
150{
151	struct smbus_ivar *devi;
152	int retval;
153
154	devi = device_get_ivars(child);
155	retval = bus_print_child_header(parent, child);
156	if (devi->addr != 0)
157		retval += printf(" at addr 0x%x", devi->addr);
158	retval += bus_print_child_footer(parent, child);
159
160	return (retval);
161}
162
163static int
164smbus_read_ivar(device_t parent, device_t child, int which, uintptr_t *result)
165{
166	struct smbus_ivar *devi;
167
168	devi = device_get_ivars(child);
169	switch (which) {
170	case SMBUS_IVAR_ADDR:
171		if (devi->addr != 0)
172			*result = devi->addr;
173		else
174			*result = -1;
175		break;
176	default:
177		return (ENOENT);
178	}
179	return (0);
180}
181
182static int
183smbus_write_ivar(device_t parent, device_t child, int which, uintptr_t value)
184{
185	struct smbus_ivar *devi;
186
187	devi = device_get_ivars(child);
188	switch (which) {
189	case SMBUS_IVAR_ADDR:
190		/* Allow to set but no change the slave address. */
191		if (devi->addr != 0)
192			return (EINVAL);
193		devi->addr = value;
194		break;
195	default:
196		return (ENOENT);
197	}
198	return (0);
199}
200
201static void
202smbus_probe_nomatch(device_t bus, device_t child)
203{
204	struct smbus_ivar *devi = device_get_ivars(child);
205
206	/*
207	 * Ignore (self-identified) devices without a slave address set.
208	 * For example, smb(4).
209	 */
210	if (devi->addr != 0)
211		device_printf(bus, "<unknown device> at addr %#x\n",
212		    devi->addr);
213}
214
215/*
216 * Device methods
217 */
218static device_method_t smbus_methods[] = {
219        /* device interface */
220        DEVMETHOD(device_probe,         smbus_probe),
221        DEVMETHOD(device_attach,        smbus_attach),
222        DEVMETHOD(device_detach,        smbus_detach),
223
224	/* bus interface */
225	DEVMETHOD(bus_add_child,	smbus_add_child),
226	DEVMETHOD(bus_hinted_child,	smbus_hinted_child),
227	DEVMETHOD(bus_probe_nomatch,	smbus_probe_nomatch),
228	DEVMETHOD(bus_child_location_str, smbus_child_location_str),
229	DEVMETHOD(bus_print_child,	smbus_print_child),
230	DEVMETHOD(bus_read_ivar,	smbus_read_ivar),
231	DEVMETHOD(bus_write_ivar,	smbus_write_ivar),
232
233	DEVMETHOD_END
234};
235
236driver_t smbus_driver = {
237        "smbus",
238        smbus_methods,
239        sizeof(struct smbus_softc),
240};
241
242devclass_t smbus_devclass;
243
244MODULE_VERSION(smbus, SMBUS_MODVER);
245