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/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 #include "smbus_if.h" 43 #include "bus_if.h" 44 45 struct smbus_ivar 46 { 47 uint8_t addr; 48 }; 49 50 /* 51 * Autoconfiguration and support routines for System Management bus 52 */ 53 54 static int 55 smbus_probe(device_t dev) 56 { 57 58 device_set_desc(dev, "System Management Bus"); 59 60 return (0); 61 } 62 63 static int 64 smbus_attach(device_t dev) 65 { 66 struct smbus_softc *sc = device_get_softc(dev); 67 68 mtx_init(&sc->lock, device_get_nameunit(dev), "smbus", MTX_DEF); 69 bus_generic_probe(dev); 70 bus_enumerate_hinted_children(dev); 71 bus_generic_attach(dev); 72 73 return (0); 74 } 75 76 static int 77 smbus_detach(device_t dev) 78 { 79 struct smbus_softc *sc = device_get_softc(dev); 80 int error; 81 82 error = bus_generic_detach(dev); 83 if (error) 84 return (error); 85 device_delete_children(dev); 86 mtx_destroy(&sc->lock); 87 88 return (0); 89 } 90 91 void 92 smbus_generic_intr(device_t dev, u_char devaddr, char low, char high, int err) 93 { 94 } 95 96 static device_t 97 smbus_add_child(device_t dev, u_int order, const char *name, int unit) 98 { 99 struct smbus_ivar *devi; 100 device_t child; 101 102 child = device_add_child_ordered(dev, order, name, unit); 103 if (child == NULL) 104 return (child); 105 devi = malloc(sizeof(struct smbus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); 106 if (devi == NULL) { 107 device_delete_child(dev, child); 108 return (NULL); 109 } 110 device_set_ivars(child, devi); 111 return (child); 112 } 113 114 static void 115 smbus_hinted_child(device_t bus, const char *dname, int dunit) 116 { 117 struct smbus_ivar *devi; 118 device_t child; 119 int addr; 120 121 addr = 0; 122 resource_int_value(dname, dunit, "addr", &addr); 123 if (addr > UINT8_MAX) { 124 device_printf(bus, "ignored incorrect slave address hint 0x%x" 125 " for %s%d\n", addr, dname, dunit); 126 return; 127 } 128 child = BUS_ADD_CHILD(bus, SMBUS_ORDER_HINTED, dname, dunit); 129 if (child == NULL) 130 return; 131 devi = device_get_ivars(child); 132 devi->addr = addr; 133 } 134 135 136 static int 137 smbus_child_location_str(device_t parent, device_t child, char *buf, 138 size_t buflen) 139 { 140 struct smbus_ivar *devi; 141 142 devi = device_get_ivars(child); 143 if (devi->addr != 0) 144 snprintf(buf, buflen, "addr=0x%x", devi->addr); 145 else if (buflen) 146 buf[0] = 0; 147 return (0); 148 } 149 150 static int 151 smbus_print_child(device_t parent, device_t child) 152 { 153 struct smbus_ivar *devi; 154 int retval; 155 156 devi = device_get_ivars(child); 157 retval = bus_print_child_header(parent, child); 158 if (devi->addr != 0) 159 retval += printf(" at addr 0x%x", devi->addr); 160 retval += bus_print_child_footer(parent, child); 161 162 return (retval); 163 } 164 165 static int 166 smbus_read_ivar(device_t parent, device_t child, int which, uintptr_t *result) 167 { 168 struct smbus_ivar *devi; 169 170 devi = device_get_ivars(child); 171 switch (which) { 172 case SMBUS_IVAR_ADDR: 173 if (devi->addr != 0) 174 *result = devi->addr; 175 else 176 *result = -1; 177 break; 178 default: 179 return (ENOENT); 180 } 181 return (0); 182 } 183 184 static int 185 smbus_write_ivar(device_t parent, device_t child, int which, uintptr_t value) 186 { 187 struct smbus_ivar *devi; 188 189 devi = device_get_ivars(child); 190 switch (which) { 191 case SMBUS_IVAR_ADDR: 192 /* Allow to set but no change the slave address. */ 193 if (devi->addr != 0) 194 return (EINVAL); 195 devi->addr = value; 196 break; 197 default: 198 return (ENOENT); 199 } 200 return (0); 201 } 202 203 static void 204 smbus_probe_nomatch(device_t bus, device_t child) 205 { 206 struct smbus_ivar *devi = device_get_ivars(child); 207 208 /* 209 * Ignore (self-identified) devices without a slave address set. 210 * For example, smb(4). 211 */ 212 if (devi->addr != 0) 213 device_printf(bus, "<unknown device> at addr %#x\n", 214 devi->addr); 215 } 216 217 /* 218 * Device methods 219 */ 220 static device_method_t smbus_methods[] = { 221 /* device interface */ 222 DEVMETHOD(device_probe, smbus_probe), 223 DEVMETHOD(device_attach, smbus_attach), 224 DEVMETHOD(device_detach, smbus_detach), 225 226 /* bus interface */ 227 DEVMETHOD(bus_add_child, smbus_add_child), 228 DEVMETHOD(bus_hinted_child, smbus_hinted_child), 229 DEVMETHOD(bus_probe_nomatch, smbus_probe_nomatch), 230 DEVMETHOD(bus_child_location_str, smbus_child_location_str), 231 DEVMETHOD(bus_print_child, smbus_print_child), 232 DEVMETHOD(bus_read_ivar, smbus_read_ivar), 233 DEVMETHOD(bus_write_ivar, smbus_write_ivar), 234 235 DEVMETHOD_END 236 }; 237 238 driver_t smbus_driver = { 239 "smbus", 240 smbus_methods, 241 sizeof(struct smbus_softc), 242 }; 243 244 devclass_t smbus_devclass; 245 246 MODULE_VERSION(smbus, SMBUS_MODVER); 247