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 $
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/device.h>
34 #include <sys/module.h>
38 #include <sys/fcntl.h>
50 int sc_count; /* >0 if device opened */
54 #define IIC_SOFTC(unit) \
55 ((struct smb_softc *)devclass_get_softc(smb_devclass, (unit)))
57 #define IIC_DEVICE(unit) \
58 (devclass_get_device(smb_devclass, (unit)))
60 static void smb_identify(driver_t *driver, device_t parent);
61 static int smb_probe(device_t);
62 static int smb_attach(device_t);
63 static int smb_detach(device_t);
65 static devclass_t smb_devclass;
67 static device_method_t smb_methods[] = {
68 /* device interface */
69 DEVMETHOD(device_identify, smb_identify),
70 DEVMETHOD(device_probe, smb_probe),
71 DEVMETHOD(device_attach, smb_attach),
72 DEVMETHOD(device_detach, smb_detach),
75 DEVMETHOD(smbus_intr, smbus_generic_intr),
80 static driver_t smb_driver = {
83 sizeof(struct smb_softc),
86 static d_open_t smbopen;
87 static d_close_t smbclose;
88 static d_ioctl_t smbioctl;
90 static struct dev_ops smb_ops = {
98 smb_identify(driver_t *driver, device_t parent)
100 if (device_find_child(parent, "smb", -1) == NULL)
101 BUS_ADD_CHILD(parent, parent, 0, "smb", -1);
105 smb_probe(device_t dev)
107 device_set_desc(dev, "SMBus generic I/O");
113 smb_attach(device_t dev)
115 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
120 bzero(sc, sizeof(struct smb_softc *));
122 sc->sc_devnode = make_dev(&smb_ops, device_get_unit(dev),
124 0600, "smb%d", device_get_unit(dev));
130 smb_detach(device_t dev)
132 struct smb_softc *sc = (struct smb_softc *)device_get_softc(dev);
135 dev_ops_remove_minor(&smb_ops, device_get_unit(dev));
141 smbopen (struct dev_open_args *ap)
143 cdev_t dev = ap->a_head.a_dev;
144 struct smb_softc *sc = IIC_SOFTC(minor(dev));
149 if (sc->sc_count != 0)
158 smbclose(struct dev_close_args *ap)
160 cdev_t dev = ap->a_head.a_dev;
161 struct smb_softc *sc = IIC_SOFTC(minor(dev));
166 if (sc->sc_count == 0)
167 /* This is not supposed to happen. */
177 smbwrite(struct dev_write_args *ap)
183 smbread(struct dev_read_args *ap)
190 smbioctl(struct dev_ioctl_args *ap)
192 cdev_t dev = ap->a_head.a_dev;
193 char buf[SMB_MAXBLOCKSIZE];
195 struct smbcmd *s = (struct smbcmd *)ap->a_data;
196 struct smb_softc *sc = IIC_SOFTC(minor(dev));
197 device_t smbdev = IIC_DEVICE(minor(dev));
208 parent = device_get_parent(smbdev);
210 /* Allocate the bus. */
211 if ((error = smbus_request_bus(parent, smbdev,
212 (ap->a_fflag & O_NONBLOCK) ? SMB_DONTWAIT : (SMB_WAIT | SMB_INTR))))
216 case SMB_QUICK_WRITE:
217 error = smbus_error(smbus_quick(parent, s->slave, SMB_QWRITE));
221 error = smbus_error(smbus_quick(parent, s->slave, SMB_QREAD));
225 error = smbus_error(smbus_sendb(parent, s->slave, s->cmd));
229 error = smbus_error(smbus_recvb(parent, s->slave, &s->cmd));
233 error = smbus_error(smbus_writeb(parent, s->slave, s->cmd,
238 error = smbus_error(smbus_writew(parent, s->slave,
239 s->cmd, s->data.word));
243 if (s->data.byte_ptr) {
244 error = smbus_error(smbus_readb(parent, s->slave,
248 error = copyout(&c, s->data.byte_ptr,
249 sizeof(*(s->data.byte_ptr)));
254 if (s->data.word_ptr) {
255 error = smbus_error(smbus_readw(parent, s->slave,
258 error = copyout(&w, s->data.word_ptr,
259 sizeof(*(s->data.word_ptr)));
265 if (s->data.process.rdata) {
266 error = smbus_error(smbus_pcall(parent, s->slave, s->cmd,
267 s->data.process.sdata, &w));
270 error = copyout(&w, s->data.process.rdata,
271 sizeof(*(s->data.process.rdata)));
277 if (s->count && s->data.byte_ptr) {
278 if (s->count > SMB_MAXBLOCKSIZE)
279 s->count = SMB_MAXBLOCKSIZE;
280 error = copyin(s->data.byte_ptr, buf, s->count);
283 error = smbus_error(smbus_bwrite(parent, s->slave,
284 s->cmd, s->count, buf));
290 if (s->count && s->data.byte_ptr) {
291 count = min(s->count, SMB_MAXBLOCKSIZE);
292 error = smbus_error(smbus_bread(parent, s->slave,
293 s->cmd, &count, buf));
296 error = copyout(buf, s->data.byte_ptr,
297 min(count, s->count));
306 smbus_release_bus(parent, smbdev);
311 DRIVER_MODULE(smb, smbus, smb_driver, smb_devclass, NULL, NULL);
312 MODULE_DEPEND(smb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
313 MODULE_VERSION(smb, 1);