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