2 * Copyright (c) 1998, 2001 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/param.h>
30 #include <sys/kernel.h>
31 #include <sys/systm.h>
32 #include <sys/module.h>
36 #include <sys/fcntl.h>
38 #include <dev/smbus/smbconf.h>
39 #include <dev/smbus/smbus.h>
40 #include <dev/smbus/smb.h>
44 #define SMB_OLD_READB _IOW('i', 7, struct smbcmd)
45 #define SMB_OLD_READW _IOW('i', 8, struct smbcmd)
46 #define SMB_OLD_PCALL _IOW('i', 9, struct smbcmd)
50 struct cdev *sc_devnode;
53 static void smb_identify(driver_t *driver, device_t parent);
54 static int smb_probe(device_t);
55 static int smb_attach(device_t);
56 static int smb_detach(device_t);
58 static devclass_t smb_devclass;
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),
73 static driver_t smb_driver = {
76 sizeof(struct smb_softc),
79 static d_ioctl_t smbioctl;
81 static struct cdevsw smb_cdevsw = {
82 .d_version = D_VERSION,
83 .d_flags = D_TRACKCLOSE,
89 smb_identify(driver_t *driver, device_t parent)
92 if (device_find_child(parent, "smb", -1) == NULL)
93 BUS_ADD_CHILD(parent, 0, "smb", -1);
97 smb_probe(device_t dev)
99 if (smbus_get_addr(dev) != -1)
102 device_set_desc(dev, "SMBus generic I/O");
103 return (BUS_PROBE_NOWILDCARD);
107 smb_attach(device_t dev)
109 struct smb_softc *sc;
110 struct make_dev_args mda;
113 sc = device_get_softc(dev);
116 make_dev_args_init(&mda);
117 mda.mda_devsw = &smb_cdevsw;
118 mda.mda_unit = device_get_unit(dev);
119 mda.mda_uid = UID_ROOT;
120 mda.mda_gid = GID_WHEEL;
122 mda.mda_si_drv1 = sc;
123 error = make_dev_s(&mda, &sc->sc_devnode, "smb%d", mda.mda_unit);
128 smb_detach(device_t dev)
130 struct smb_softc *sc;
132 sc = device_get_softc(dev);
133 destroy_dev(sc->sc_devnode);
138 smbioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, struct thread *td)
140 char buf[SMB_MAXBLOCKSIZE];
142 struct smbcmd *s = (struct smbcmd *)data;
143 struct smb_softc *sc = dev->si_drv1;
144 device_t smbdev = sc->sc_dev;
150 * If a specific slave device is being used, override any passed-in
153 unit = dev2unit(dev);
155 s->slave = unit & 0x03ff;
157 parent = device_get_parent(smbdev);
159 /* Make sure that LSB bit is cleared. */
163 /* Allocate the bus. */
164 if ((error = smbus_request_bus(parent, smbdev,
165 (flags & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
169 case SMB_QUICK_WRITE:
170 error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
174 error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
178 error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
182 error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
186 error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
191 error = smbus_error(smbus_writew(parent, s->slave,
192 s->cmd, s->wdata.word));
197 /* NB: for SMB_OLD_READB the read data goes to rbuf only. */
198 error = smbus_error(smbus_readb(parent, s->slave, s->cmd,
202 if (s->rbuf && s->rcount >= 1) {
203 error = copyout(&s->rdata.byte, s->rbuf, 1);
210 /* NB: for SMB_OLD_READW the read data goes to rbuf only. */
211 error = smbus_error(smbus_readw(parent, s->slave, s->cmd,
215 if (s->rbuf && s->rcount >= 2) {
216 buf[0] = (u_char)s->rdata.word;
217 buf[1] = (u_char)(s->rdata.word >> 8);
218 error = copyout(buf, s->rbuf, 2);
225 /* NB: for SMB_OLD_PCALL the read data goes to rbuf only. */
226 error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
227 s->wdata.word, &s->rdata.word));
230 if (s->rbuf && s->rcount >= 2) {
231 buf[0] = (u_char)s->rdata.word;
232 buf[1] = (u_char)(s->rdata.word >> 8);
233 error = copyout(buf, s->rbuf, 2);
244 if (s->wcount > SMB_MAXBLOCKSIZE)
245 s->wcount = SMB_MAXBLOCKSIZE;
247 error = copyin(s->wbuf, buf, s->wcount);
250 error = smbus_error(smbus_bwrite(parent, s->slave, s->cmd,
259 if (s->rcount > SMB_MAXBLOCKSIZE)
260 s->rcount = SMB_MAXBLOCKSIZE;
261 error = smbus_error(smbus_bread(parent, s->slave, s->cmd,
265 if (s->rcount > bcount)
267 error = copyout(buf, s->rbuf, s->rcount);
274 smbus_release_bus(parent, smbdev);
279 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);
280 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
281 MODULE_VERSION(smb, 1);