2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, 2001 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
38 #include <sys/fcntl.h>
40 #include <dev/smbus/smbconf.h>
41 #include <dev/smbus/smbus.h>
42 #include <dev/smbus/smb.h>
46 #define SMB_OLD_READB _IOW('i', 7, struct smbcmd)
47 #define SMB_OLD_READW _IOW('i', 8, struct smbcmd)
48 #define SMB_OLD_PCALL _IOW('i', 9, struct smbcmd)
52 struct cdev *sc_devnode;
55 static void smb_identify(driver_t *driver, device_t parent);
56 static int smb_probe(device_t);
57 static int smb_attach(device_t);
58 static int smb_detach(device_t);
60 static device_method_t smb_methods[] = {
61 /* device interface */
62 DEVMETHOD(device_identify, smb_identify),
63 DEVMETHOD(device_probe, smb_probe),
64 DEVMETHOD(device_attach, smb_attach),
65 DEVMETHOD(device_detach, smb_detach),
68 DEVMETHOD(smbus_intr, smbus_generic_intr),
72 static driver_t smb_driver = {
75 sizeof(struct smb_softc),
78 static d_ioctl_t smbioctl;
80 static struct cdevsw smb_cdevsw = {
81 .d_version = D_VERSION,
82 .d_flags = D_TRACKCLOSE,
88 smb_identify(driver_t *driver, device_t parent)
91 if (device_find_child(parent, "smb", -1) == NULL)
92 BUS_ADD_CHILD(parent, 0, "smb", -1);
96 smb_probe(device_t dev)
98 if (smbus_get_addr(dev) != -1)
101 device_set_desc(dev, "SMBus generic I/O");
102 return (BUS_PROBE_NOWILDCARD);
106 smb_attach(device_t dev)
108 struct smb_softc *sc;
109 struct make_dev_args mda;
112 sc = device_get_softc(dev);
115 make_dev_args_init(&mda);
116 mda.mda_devsw = &smb_cdevsw;
117 mda.mda_unit = device_get_unit(dev);
118 mda.mda_uid = UID_ROOT;
119 mda.mda_gid = GID_WHEEL;
121 mda.mda_si_drv1 = sc;
122 error = make_dev_s(&mda, &sc->sc_devnode, "smb%d", mda.mda_unit);
127 smb_detach(device_t dev)
129 struct smb_softc *sc;
131 sc = device_get_softc(dev);
132 destroy_dev(sc->sc_devnode);
137 smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
139 char buf[SMB_MAXBLOCKSIZE];
141 struct smbcmd *s = (struct smbcmd *)data;
142 struct smb_softc *sc = dev->si_drv1;
143 device_t smbdev = sc->sc_dev;
149 * If a specific slave device is being used, override any passed-in
152 unit = dev2unit(dev);
154 s->slave = unit & 0x03ff;
156 parent = device_get_parent(smbdev);
158 /* Make sure that LSB bit is cleared. */
162 /* Allocate the bus. */
163 if ((error = smbus_request_bus(parent, smbdev,
164 (flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
168 case SMB_QUICK_WRITE:
169 error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
173 error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
177 error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
181 error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
185 error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
190 error = smbus_error(smbus_writew(parent, s->slave,
191 s->cmd, s->wdata.word));
196 /* NB: for SMB_OLD_READB the read data goes to rbuf only. */
197 error = smbus_error(smbus_readb(parent, s->slave, s->cmd,
201 if (s->rbuf && s->rcount >= 1) {
202 error = copyout(&s->rdata.byte, s->rbuf, 1);
209 /* NB: for SMB_OLD_READW the read data goes to rbuf only. */
210 error = smbus_error(smbus_readw(parent, s->slave, s->cmd,
214 if (s->rbuf && s->rcount >= 2) {
215 buf[0] = (u_char)s->rdata.word;
216 buf[1] = (u_char)(s->rdata.word >> 8);
217 error = copyout(buf, s->rbuf, 2);
224 /* NB: for SMB_OLD_PCALL the read data goes to rbuf only. */
225 error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
226 s->wdata.word, &s->rdata.word));
229 if (s->rbuf && s->rcount >= 2) {
230 buf[0] = (u_char)s->rdata.word;
231 buf[1] = (u_char)(s->rdata.word >> 8);
232 error = copyout(buf, s->rbuf, 2);
243 if (s->wcount > SMB_MAXBLOCKSIZE)
244 s->wcount = SMB_MAXBLOCKSIZE;
246 error = copyin(s->wbuf, buf, s->wcount);
249 error = smbus_error(smbus_bwrite(parent, s->slave, s->cmd,
258 if (s->rcount > SMB_MAXBLOCKSIZE)
259 s->rcount = SMB_MAXBLOCKSIZE;
260 error = smbus_error(smbus_bread(parent, s->slave, s->cmd,
264 if (s->rcount > bcount)
266 error = copyout(buf, s->rbuf, s->rcount);
273 smbus_release_bus(parent, smbdev);
278 DRIVER_MODULE(smb, smbus, smb_driver, 0, 0);
279 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
280 MODULE_VERSION(smb, 1);