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$"); 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 #include <sys/lock.h> 34 #include <sys/module.h> 35 #include <sys/mutex.h> 36 #include <sys/bus.h> 37 38 #include <dev/smbus/smbconf.h> 39 #include <dev/smbus/smbus.h> 40 41 #include "smbus_if.h" 42 #include "bus_if.h" 43 44 45 /* 46 * Autoconfiguration and support routines for System Management bus 47 */ 48 49 /* 50 * Device methods 51 */ 52 static int smbus_probe(device_t); 53 static int smbus_attach(device_t); 54 static int smbus_detach(device_t); 55 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, 61 uintptr_t *result); 62 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), 68 69 /* bus interface */ 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), 75 76 DEVMETHOD_END 77 }; 78 79 driver_t smbus_driver = { 80 "smbus", 81 smbus_methods, 82 sizeof(struct smbus_softc), 83 }; 84 85 devclass_t smbus_devclass; 86 87 /* 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 90 * are alive. 91 */ 92 static int 93 smbus_probe(device_t dev) 94 { 95 96 device_set_desc(dev, "System Management Bus"); 97 98 return (0); 99 } 100 101 static int 102 smbus_attach(device_t dev) 103 { 104 struct smbus_softc *sc = device_get_softc(dev); 105 unsigned char addr; 106 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]); 112 } 113 bus_generic_attach(dev); 114 115 return (0); 116 } 117 118 static int 119 smbus_detach(device_t dev) 120 { 121 struct smbus_softc *sc = device_get_softc(dev); 122 int error; 123 124 error = bus_generic_detach(dev); 125 if (error) 126 return (error); 127 mtx_destroy(&sc->lock); 128 129 return (0); 130 } 131 132 void 133 smbus_generic_intr(device_t dev, u_char devaddr, char low, char high, int err) 134 { 135 } 136 137 static void 138 smbus_probe_device(device_t dev, u_char* addr) 139 { 140 device_t child; 141 int error; 142 u_char cmd; 143 u_char buf[2]; 144 145 cmd = 0x01; 146 error = smbus_trans(dev, *addr, cmd, 147 SMB_TRANS_NOCNT | SMB_TRANS_NOREPORT, 148 NULL, 0, buf, 1, NULL); 149 if (error == 0) { 150 if (bootverbose) 151 device_printf(dev, "Probed address 0x%02x\n", *addr); 152 child = device_add_child(dev, NULL, -1); 153 device_set_ivars(child, addr); 154 } 155 } 156 157 static int 158 smbus_child_location_str(device_t parent, device_t child, char *buf, 159 size_t buflen) 160 { 161 unsigned char *addr; 162 163 addr = device_get_ivars(child); 164 if (addr) 165 snprintf(buf, buflen, "addr=0x%x", *addr); 166 else if (buflen) 167 buf[0] = 0; 168 return (0); 169 } 170 171 static int 172 smbus_print_child(device_t parent, device_t child) 173 { 174 unsigned char *addr; 175 int retval; 176 177 addr = device_get_ivars(child); 178 retval = bus_print_child_header(parent, child); 179 if (addr) 180 retval += printf(" at addr 0x%x", *addr); 181 retval += bus_print_child_footer(parent, child); 182 183 return (retval); 184 } 185 186 static int 187 smbus_read_ivar(device_t parent, device_t child, int which, 188 uintptr_t *result) 189 { 190 unsigned char *addr; 191 192 addr = device_get_ivars(child); 193 switch (which) { 194 case SMBUS_IVAR_ADDR: 195 *result = (addr == NULL) ? -1 : *addr; 196 break; 197 default: 198 return (ENOENT); 199 } 200 return (0); 201 } 202 203 MODULE_VERSION(smbus, SMBUS_MODVER); 204