2 * Copyright (c) 1998, 2001 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/mutex.h>
38 #include <dev/smbus/smbconf.h>
39 #include <dev/smbus/smbus.h>
46 * Autoconfiguration and support routines for System Management bus
52 static int smbus_probe(device_t);
53 static int smbus_attach(device_t);
54 static int smbus_detach(device_t);
56 static int smbus_child_location_str(device_t parent, device_t child,
57 char *buf, size_t buflen);
58 static int smbus_print_child(device_t parent, device_t child);
59 static void smbus_probe_device(device_t dev, u_char* addr);
60 static int smbus_read_ivar(device_t parent, device_t child, int which,
63 static device_method_t smbus_methods[] = {
64 /* device interface */
65 DEVMETHOD(device_probe, smbus_probe),
66 DEVMETHOD(device_attach, smbus_attach),
67 DEVMETHOD(device_detach, smbus_detach),
70 DEVMETHOD(bus_add_child, bus_generic_add_child),
71 DEVMETHOD(bus_child_location_str, smbus_child_location_str),
72 DEVMETHOD(bus_driver_added, bus_generic_driver_added),
73 DEVMETHOD(bus_print_child, smbus_print_child),
74 DEVMETHOD(bus_read_ivar, smbus_read_ivar),
79 driver_t smbus_driver = {
82 sizeof(struct smbus_softc),
85 devclass_t smbus_devclass;
88 * At 'probe' time, we add all the devices which we know about to the
89 * bus. The generic attach routine will probe and attach them if they
93 smbus_probe(device_t dev)
96 device_set_desc(dev, "System Management Bus");
102 smbus_attach(device_t dev)
104 struct smbus_softc *sc = device_get_softc(dev);
107 mtx_init(&sc->lock, device_get_nameunit(dev), "smbus", MTX_DEF);
108 bus_generic_probe(dev);
109 for (addr = SMBUS_ADDR_MIN; addr < SMBUS_ADDR_MAX; ++addr) {
110 sc->addrs[addr] = addr;
111 smbus_probe_device(dev, &sc->addrs[addr]);
113 bus_generic_attach(dev);
119 smbus_detach(device_t dev)
121 struct smbus_softc *sc = device_get_softc(dev);
124 error = bus_generic_detach(dev);
127 mtx_destroy(&sc->lock);
133 smbus_generic_intr(device_t dev, u_char devaddr, char low, char high, int err)
138 smbus_probe_device(device_t dev, u_char* addr)
146 error = smbus_trans(dev, *addr, cmd,
147 SMB_TRANS_NOCNT | SMB_TRANS_NOREPORT,
148 NULL, 0, buf, 1, NULL);
151 device_printf(dev, "Probed address 0x%02x\n", *addr);
152 child = device_add_child(dev, NULL, -1);
153 device_set_ivars(child, addr);
158 smbus_child_location_str(device_t parent, device_t child, char *buf,
163 addr = device_get_ivars(child);
165 snprintf(buf, buflen, "addr=0x%x", *addr);
172 smbus_print_child(device_t parent, device_t child)
177 addr = device_get_ivars(child);
178 retval = bus_print_child_header(parent, child);
180 retval += printf(" at addr 0x%x", *addr);
181 retval += bus_print_child_footer(parent, child);
187 smbus_read_ivar(device_t parent, device_t child, int which,
192 addr = device_get_ivars(child);
194 case SMBUS_IVAR_ADDR:
195 *result = (addr == NULL) ? -1 : *addr;
203 MODULE_VERSION(smbus, SMBUS_MODVER);