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