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
26 * $FreeBSD: src/sys/dev/smbus/smb.c,v 1.34.8.2 2006/09/22 19:19:16 jhb Exp $
27 * $DragonFly: src/sys/bus/smbus/smb.c,v 1.9 2006/09/10 01:26:33 dillon Exp $
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/module.h>
39 #include <sys/fcntl.h>
51 int sc_count; /* >0 if device opened */
55 #define IIC_SOFTC(unit) \
56 ((struct smb_softc *)devclass_get_softc(smb_devclass, (unit)))
58 #define IIC_DEVICE(unit) \
59 (devclass_get_device(smb_devclass, (unit)))
61 static void smb_identify(driver_t *driver, device_t parent);
62 static int smb_probe(device_t);
63 static int smb_attach(device_t);
64 static int smb_detach(device_t);
66 static devclass_t smb_devclass;
68 static device_method_t smb_methods[] = {
69 /* device interface */
70 DEVMETHOD(device_identify, smb_identify),
71 DEVMETHOD(device_probe, smb_probe),
72 DEVMETHOD(device_attach, smb_attach),
73 DEVMETHOD(device_detach, smb_detach),
76 DEVMETHOD(smbus_intr, smbus_generic_intr),
81 static driver_t smb_driver = {
84 sizeof(struct smb_softc),
87 static d_open_t smbopen;
88 static d_close_t smbclose;
89 static d_ioctl_t smbioctl;
91 #define CDEV_MAJOR 106
92 static struct dev_ops smb_ops = {
93 { "smb", CDEV_MAJOR, 0 },
100 smb_identify(driver_t *driver, device_t parent)
102 if (device_find_child(parent, "smb", -1) == NULL)
103 BUS_ADD_CHILD(parent, parent, 0, "smb", -1);
107 smb_probe(device_t dev)
109 device_set_desc(dev, "SMBus generic I/O");
115 smb_attach(device_t dev)
117 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
122 bzero(sc, sizeof(struct smb_softc *));
124 sc->sc_devnode = make_dev(&smb_ops, device_get_unit(dev),
126 0600, "smb%d", device_get_unit(dev));
132 smb_detach(device_t dev)
134 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
137 dev_ops_remove_minor(&smb_ops, device_get_unit(dev));
143 smbopen (struct dev_open_args *ap)
145 cdev_t dev = ap->a_head.a_dev;
146 struct smb_softc *sc = IIC_SOFTC(minor(dev));
151 if (sc->sc_count != 0)
160 smbclose(struct dev_close_args *ap)
162 cdev_t dev = ap->a_head.a_dev;
163 struct smb_softc *sc = IIC_SOFTC(minor(dev));
168 if (sc->sc_count == 0)
169 /* This is not supposed to happen. */
179 smbwrite(struct dev_write_args *ap)
185 smbread(struct dev_read_args *ap)
192 smbioctl(struct dev_ioctl_args *ap)
194 cdev_t dev = ap->a_head.a_dev;
195 char buf[SMB_MAXBLOCKSIZE];
197 struct smbcmd *s = (struct smbcmd *)ap->a_data;
198 struct smb_softc *sc = IIC_SOFTC(minor(dev));
199 device_t smbdev = IIC_DEVICE(minor(dev));
210 parent = device_get_parent(smbdev);
212 /* Allocate the bus. */
213 if ((error = smbus_request_bus(parent, smbdev,
214 (ap->a_fflag & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
218 case SMB_QUICK_WRITE:
219 error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
223 error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
227 error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
231 error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
235 error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
240 error = smbus_error(smbus_writew(parent, s->slave,
241 s->cmd, s->data.word));
245 if (s->data.byte_ptr) {
246 error = smbus_error(smbus_readb(parent, s->slave,
250 error = copyout(&c, s->data.byte_ptr,
251 sizeof(*(s->data.byte_ptr)));
256 if (s->data.word_ptr) {
257 error = smbus_error(smbus_readw(parent, s->slave,
260 error = copyout(&w, s->data.word_ptr,
261 sizeof(*(s->data.word_ptr)));
267 if (s->data.process.rdata) {
268 error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
269 s->data.process.sdata, &w));
272 error = copyout(&w, s->data.process.rdata,
273 sizeof(*(s->data.process.rdata)));
279 if (s->count && s->data.byte_ptr) {
280 if (s->count > SMB_MAXBLOCKSIZE)
281 s->count = SMB_MAXBLOCKSIZE;
282 error = copyin(s->data.byte_ptr, buf, s->count);
285 error = smbus_error(smbus_bwrite(parent, s->slave,
286 s->cmd, s->count, buf));
292 if (s->count && s->data.byte_ptr) {
293 count = min(s->count, SMB_MAXBLOCKSIZE);
294 error = smbus_error(smbus_bread(parent, s->slave,
295 s->cmd, &count, buf));
298 error = copyout(buf, s->data.byte_ptr,
299 min(count, s->count));
308 smbus_release_bus(parent, smbdev);
313 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, 0, 0);
314 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
315 MODULE_VERSION(smb, 1);